Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 08-06-2010, 07:38 PM need help with array
ZoC
Skilled Talker

Posts: 68
Trades: 0
hello there, this is my command

PHP Code:
<?php

 $allowed_refferer 
= array(
  
'http://www.google.com',
  
'http://www.yahoo.com'
 
);

 if (
in_array($_SERVER['HTTP_REFERER'], $allowed_refferer)) {
  echo 
'a';
 } else {
  echo 
'b';
 }

?>
i have one problem hope someone can help me with this...
if the refferer is http://www.google.com is show me echo a

but if the refferer is http://www.google.com/search... is not working anymore ... how i can make this work ?

i want to use array becouse is more easy to add the links becouse before i use preg_match ...
ZoC is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-06-2010, 09:47 PM Re: need help with array
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Soooo basically you want a PHP script that will say something like "Hello Googler" if the user came to your site off of a Google search?

In that case, I'll try to have something working for you shortly.

Taken from php.net's $SERVER page:
Quote:
Originally Posted by PHP.net
The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.
K done, it searches for a name, so just put anything in there where 'google' is and it'll find it.

PHP Code:
<?php

function is_ref_legal($ref) {
  return 
preg_match("/google/i",$ref);
}

 if (
is_ref_legal($_SERVER['HTTP_REFERER'])=="1") {
  echo 
'a'//Was found
 
} else {
  echo 
'b'//Was not found
 
}
?>
Keep in mind that if you want to add a slash (/) to the preg_match (example: directory/subdirectory) into (directory\/subdirectory), because a slash will end the preg_match.
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!

Last edited by Physicsguy; 08-07-2010 at 09:53 AM..
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 08-07-2010, 01:01 PM Re: need help with array
phpdasan's Avatar
Experienced Talker

Posts: 37
Name: Karthick B
Trades: 0
Try this...

PHP Code:
 
$allowed_refferer 
= array(
  
'google',
  
'yahoo'
 
);
 
$refer explode('.',$_SERVER['HTTP_REFERER']);
 if (
in_array($refer[1], $allowed_refferer)) {
  echo 
'a';
 } else {
  echo 
'b';
 } 
__________________
There is no secret ingredient.
phpdasan is offline
Reply With Quote
View Public Profile
 
Old 08-07-2010, 02:19 PM Re: need help with array
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Ah, phpdasan's solution is better, go with his
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 08-07-2010, 06:45 PM Re: need help with array
Average Talker

Posts: 25
Name: Stephen
Location: Arizona
Trades: 0
Hm.. what about www . google . notreallygoogle . com. Maybe instead of using the second element, use the second to last element.

So use $refer[count($refer)-2] instead of $refer[1]
__________________

Please login or register to view this content. Registration is FREE
and
Please login or register to view this content. Registration is FREE
GetGamesHere is offline
Reply With Quote
View Public Profile Visit GetGamesHere's homepage!
 
Old 08-11-2010, 09:29 PM Re: need help with array
Average Talker

Posts: 26
Name: rutty
Trades: 0
Quote:
<?php
// Create a simple array.
$array = array(1, 2, 3, 4, 5);
print_r($array);

// Now delete every item, but leave the array itself intact:
foreach ($array as $i => $value) {
unset(
$array[$i]);
}
print_r($array);

// Append an item (note that the new key is 5, instead of 0).
$array[] = 6;
print_r($array);

// Re-index:
$array = array_values($array);
$array[] = 7;
print_r($array);
?>
These things might be helpful to you.
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
itchair is offline
Reply With Quote
View Public Profile
 
Old 08-12-2010, 07:34 AM Re: need help with array
Kelpie's Avatar
Skilled Talker

Posts: 82
Name: Andrew
Location: SW Scotland
Trades: 0
GetGamesHere's solution would accept google and yahoo in the US as referrers, but not their sites in other countries; e.g. google.com.au or yahoo.co.uk
phpdasan's would accept these other sites, but has the problem GetGamesHere raised.

If either of these are fine for your site, then go with whichever meets your needs, but if you want to accept international referrals AND block fake referrals the condition gets a bit more complex
PHP Code:
$allowed_refferer = array(
 
'google',
 
'yahoo'
);
 
$refer explode('.',$_SERVER['HTTP_REFERER']);
if (((
in_array($refer[count($refer)-2], $allowed_refferer)) && (in_array($refer[count($refer)-1], 'com'))) || ((in_array($refer[count($refer)-3], $allowed_refferer)) && ((in_array($refer[count($refer)-2], 'com')) || (in_array($refer[count($refer)-2], 'co'))))) {
echo 
'a';
}
else {
echo 
'b';

Should do it (I think I've got all the brackets in the right places )
Kelpie is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to need help with array
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.13754 seconds with 12 queries