PHP Keyword Alert Script

The Scarlet Pimp

Supreme Member
Joined
Apr 2, 2008
Messages
1,416
Reaction score
4,457
This is a very simple script that will send you an email every time somebody views the web page it's installed on. The email will show you the exact keywords and search engine the visitor used to find your web page. Plus it will also show you that visitor's IP number.

This script can be helpful if you use Adwords since you'll know which words, phrases, and misspellings are being used. Or you can just add the words to your web page to (hopefully) increase your overall rank. :D

You'll need to adjust this to show the correct time when the page was viewed. Replace 'xxx' with the time difference between where you are and where the
Server is.

For example, if you live in New York and you host with Lunar Pages you would
replace 'xxx' with the numeral '3' because there's a three hour time difference between NY and California. But if you live in Cali. then you'd use the numeral '0' because you're in the same time zone.

Add your email address and the time difference (if any) and place this at the
bottom of the page. You should also add a filter to your email account so the
messages get filed into their appropriate folders. Otherwise your main email account will get flooded with alert messages!

An easy way to do this is to file the alerts according to message subject,
which you can easily change to anything that you like. If you have any problems, post them here. But this is pretty straight forward so you should
not have any problems.

$recipient = "[email protected]"; Put your email here.

$hourdiff = "XXX"; Enter time difference here.

$subject = "Visitor Alert: Home Page"; Change this as you will.


Code:
<?
$myserver = "http://".$HTTP_HOST.$_SERVER[REQUEST_URI];
$ip = $_SERVER['REMOTE_ADDR'];
$cname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$fullhost = gethostbyaddr($ip);
$host = preg_replace("/^[^.]+./", "*.", $fullhost);
$hourdiff = "XXX"; 
$timeadjust = ($hourdiff * 60 * 60); 
$melbdate = date("D, M. jS, Y [g:i A]",time() + $timeadjust); 
$recipient = "[email protected]";
$subject = "Visitor Alert: Home Page";
$message = "--------------------------------<br>
*** SENDER INFO ***
<p>
ISP Address:<br> 
<a href='http://ws.arin.net/cgi-bin/whois.pl?queryinput=$_SERVER[REMOTE_ADDR]'>$_SERVER[REMOTE_ADDR]</a>
<p>
ISP Name:<br> 
$cname
<p>
Browser Type:<br> 
$_SERVER[HTTP_USER_AGENT]
<p>
Page Referer:<br> 
<a href=$_SERVER[HTTP_REFERER]>$_SERVER[HTTP_REFERER]</a>
<p>
Page Viewed:<br>
<a href=$myserver>$myserver</a>
<p>
Date and Time Sent:<br>
$melbdate<br>
--------------------------------";
$message = stripslashes($message);
$headers = "From: Alert <[email protected]>\r\n";
$headers.= "MIME-Version: 1.0\r\n";
$headers.= "Content-type: text/html; charset=ISO-8859-1\r\n";
mail($recipient,$subject,$message,$headers);
?>
 
awesome , might prove useful.

thanks and reps to you bro.
 
This looks awesome.

Do we just add it to the page html?
 
post it just before the closing </body> tag and it
should work fine. it only works if the page extension
is ".php"

edit and paste that script onto a page named,
'test.php' and then view the page. you'll get an
email showing your info.
 
Last edited:
Thanks for the script, I will have to test it.:)

K
 
I just installed it on my site, works beautifully. I have one question though. If i visit www. mysite. com via google, I get an e-mail. and if I then go to www. mysite. com/ page1 I also get an e-mail, so if a visitor visits 10 pages on my site, I will get 10 e-mails.

Is there any way I can limit it? maybe only get an e-mail if I get a visitor from a refering site, rather than from other pages on my site I currently have it installed in my header.php on my wordpress blog.
 
This is nice...but "why email"?
Seems like a pain to get all this via tons of emails. I think the script could easily be altered to log the info to a text file on the server or into a db.

Just my 2 cents.
 
I just installed it on my site, works beautifully. I have one question though. If i visit www. mysite. com via google, I get an e-mail. and if I then go to www. mysite. com/ page1 I also get an e-mail, so if a visitor visits 10 pages on my site, I will get 10 e-mails.

Is there any way I can limit it? maybe only get an e-mail if I get a visitor from a refering site, rather than from other pages on my site I currently have it installed in my header.php on my wordpress blog.

hee! hee! i'd never put it on a blog, in fact you should remove it because it may overload your server and make you look like you're bulk mailing spam.

hmmm... i dunno how to make it do as you ask, but that is a good question. :confused:
 
This is nice...but "why email"?
Seems like a pain to get all this via tons of emails. I think the script could easily be altered to log the info to a text file on the server or into a db.

Just my 2 cents.

good point! i will work on that change... :cool:
 
I think emails is still a very good feature... but instead of an email for every visitor maybe you could group it by let's say a timeframe of your choice? like get an email update with all of the visitors info every hour or every day

good point! i will work on that change... :cool:
 
I think emails is still a very good feature... but instead of an email for every visitor maybe you could group it by let's say a timeframe of your choice? like get an email update with all of the visitors info every hour or every day

hmmm... that would require a table, and i know zip about those...
:prof01:
 
This is nice...but "why email"?
Seems like a pain to get all this via tons of emails. I think the script could easily be altered to log the info to a text file on the server or into a db.

Just my 2 cents.

here ya go! :D
change the 'XXX' to a number that reflects the time diff. between where you are and where your server is!

Code:
<?php 
$myserver = "http://".$HTTP_HOST.$_SERVER[REQUEST_URI];
$ip = $_SERVER['REMOTE_ADDR'];
$cname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$fullhost = gethostbyaddr($ip);
$host = preg_replace("/^[^.]+./", "*.", $fullhost);
$hourdiff = "XXX"; 
$timeadjust = ($hourdiff * 60 * 60); 
$melbdate = date("D. M. j, Y [g:i A]",time() + $timeadjust);
$subject = "<font color=#990000><b>Visitor Alert: Index Page</b></font>\n";
$message = "<br />--------------------------------<br />
*** SENDER INFO ***
<p>
ISP Address:<br />
<a href='http://ws.arin.net/cgi-bin/whois.pl?queryinput=$_SERVER[REMOTE_ADDR]'>$_SERVER[REMOTE_ADDR]</a>
<p>
ISP Name:<br />
$cname
<p>
Browser Type:<br />
$_SERVER[HTTP_USER_AGENT]
<p>
Page Referer:<br />
<a href=$_SERVER[HTTP_REFERER]>$_SERVER[HTTP_REFERER]</a>
<p>
Page Viewed:<br />
<a href=$myserver>$myserver</a>
<p>
Date and Time Sent:<br />
$melbdate<br />
--------------------------------
<br /><br /><br />\n\n\n";
$message = stripslashes($message);
$myFile = "log.php";
$fh = fopen($myFile, 'a') or die ('cannot open the log file, check permission');
$stringData = "$subject$message";
fwrite($fh, $stringData);
fclose($fh);
?>
 
Thanks for the script, looks nice, ill take a loot further into it later tonight.
 
Back
Top