Jump to content

google refferer


chrismarsden

Recommended Posts

Hi guys, can you help me...

 

im using the below code and i have been trying to figure out how to make it so that if a known ip searchs for my site it gives an output of "from the office" instead of the IP... any ideas?

 

 <?php

$name="Chris"; 
$email_address="email address here";
$email_address2="email address here";
$keywords="keywords here";
$referrer=$_SERVER['HTTP_REFERER'];

$ip=$_SERVER['REMOTE_ADDR'];



if( (stristr($referrer, "google")) && (stristr($referrer, "search")) ) {

   parse_str($referrer, $output);
   $keywords=$output['q'];

   $email_message="A visitor just arrived to the website after searching for '$keywords', Their IP address is '$ip'. ($referrer)";

   mail ("$email_address","Google referred a visitor","$email_message");
     mail ("$email_address2","Google referred a visitor","$email_message");
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/202022-google-refferer/
Share on other sites

I'd use in_array to lookup the IP address within an array of known IPs. You could also extend it to store the 'from' text and have multiple known IPs:

 

$known_ips = array(
    '123...' => 'from the office',
    // etc.
);

$ip = $_SERVER['REMOTE_ADDR'];

if (in_array($ip, $known_ips))
{
    $ip = $known_ips[$ip];
    // you may want to use a different var name here now?
}

 

You could even pull the list in dynamically from another data source (e.g. a database).

Link to comment
https://forums.phpfreaks.com/topic/202022-google-refferer/#findComment-1059453
Share on other sites

so the code would look like:

 

  <?php

$name="Chris"; // add your name here
$email_address=""; 
$email_address2=""; 
$keywords="";
$referrer=$_SERVER['HTTP_REFERER'];

$known_ips = array(
    'IP here' => 'from the office',
);

$ip = $_SERVER['REMOTE_ADDR'];

if (in_array($ips, $known_ips))
{
    $ip = $known_ips[$ips];

}


if( (stristr($referrer, "google")) && (stristr($referrer, "search")) ) {

   parse_str($referrer, $output);
   $keywords=$output['q'];

   $email_message="A visitor just arrived to the website after searching for '$keywords', Their IP address is '$ip'. ($referrer)";

   mail ("$email_address","Google referred a visitor","$email_message");
     mail ("$email_address2","Google referred a visitor","$email_message");
}
?>

Link to comment
https://forums.phpfreaks.com/topic/202022-google-refferer/#findComment-1059460
Share on other sites

Ahh, of course. Sorry my bad. I started using in_array() but when I thought about extending it to store the from text I modified the array to store the IP as the key, but I should have also switched to using array_key_exists:

 

$known_ips = array(
    '123...' => 'from the office',
    // etc.
);

$ip = $_SERVER['REMOTE_ADDR'];

if (array_key_exists($ip, $known_ips))
{
    $ip = $known_ips[$ip];
    // you may want to use a different var name here now?
}

 

That should work for you.

Link to comment
https://forums.phpfreaks.com/topic/202022-google-refferer/#findComment-1059509
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.