chrismarsden Posted May 17, 2010 Share Posted May 17, 2010 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"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/202022-google-refferer/ Share on other sites More sharing options...
chrismarsden Posted May 17, 2010 Author Share Posted May 17, 2010 i know it will be an if statement but not sure how to write it... any ideas anyone? Quote Link to comment https://forums.phpfreaks.com/topic/202022-google-refferer/#findComment-1059447 Share on other sites More sharing options...
Adam Posted May 17, 2010 Share Posted May 17, 2010 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). Quote Link to comment https://forums.phpfreaks.com/topic/202022-google-refferer/#findComment-1059453 Share on other sites More sharing options...
chrismarsden Posted May 17, 2010 Author Share Posted May 17, 2010 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"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/202022-google-refferer/#findComment-1059460 Share on other sites More sharing options...
chrismarsden Posted May 17, 2010 Author Share Posted May 17, 2010 just realised this does not work... im a noob... can you help me out lol Quote Link to comment https://forums.phpfreaks.com/topic/202022-google-refferer/#findComment-1059463 Share on other sites More sharing options...
Adam Posted May 17, 2010 Share Posted May 17, 2010 Can you be more specific with how it doesn't work? Quote Link to comment https://forums.phpfreaks.com/topic/202022-google-refferer/#findComment-1059474 Share on other sites More sharing options...
chrismarsden Posted May 17, 2010 Author Share Posted May 17, 2010 it does not output the line "from the office" in the email, just the IP as it always has done. Quote Link to comment https://forums.phpfreaks.com/topic/202022-google-refferer/#findComment-1059477 Share on other sites More sharing options...
Adam Posted May 17, 2010 Share Posted May 17, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/202022-google-refferer/#findComment-1059509 Share on other sites More sharing options...
chrismarsden Posted May 17, 2010 Author Share Posted May 17, 2010 that seems to work brill... Quote Link to comment https://forums.phpfreaks.com/topic/202022-google-refferer/#findComment-1059526 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.