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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.