Location: PHPKode > projects > Modular Site Manager > htdocs/tutorials/PHP-11.php
Ok, so you have a site and want to know who visits your site, how, what browser they use, etc. Well this is the perfect tutorial for you! <br />
<br />
Here's what you'll learn from this:<br />
1. Use cPanel to create a cron job.<br />
2. Use flat files to store information.<br />
3. Use the php mail(); function to send HTML.<br />
4. How to find someones IP address.<br />
5. How to get someones browser, and simplify its output <img src="/vou-flooder/smileys/happy.gif" alt="Happy" style="border:none;" /><br />
<br />
Ok, first of all, you want to create a file called referrerlogger.php (or some other name you think you can remember what it is).<br />
toss the following code into it:<br />
<div class="code"><strong>CODE:</strong><br /><hr /><?php xhtml_highlight('<?<br />
include "userbrowser.php";<br />
	$referer = $HTTP_REFERER;<br />
	$date = date("n-j-y @ g:i A");<br />
	$fp = fopen("referrers.htm","a");<br />
	$wstring = "<p><strong>Date:</strong> ".$date."<br /> <strong>Referer:</strong> ".$referer."<br /><strong>Requested:</strong> http://www.mysite.tld".$_SERVER["REQUEST_URI"]."<br /><strong>Method:</strong> ".$_SERVER["REQUEST_METHOD"]."<br /><strong>IP:</strong> ". $REMOTE_ADDR ."<br /><strong>Browser:</strong> ".$endb."</p>n";<br />
	fwrite($fp,$wstring);<br />
	fclose($fp);<br />
?>'); ?></div><br />
<br />
It's all pretty basic. You include the file that we'll later create that contains the stuff to identify the users browser. Then you create the date, then open the file that we're writing to, then define everything we're writing, then write it, and close the file! Simple, no?<br />
<br />
This logs the date &amp; time that the person visited, what page they visited, what method they used (like GET, POST, and HEAD [not really imporant]), their IP, and their browser.<br />
<br />
Next we want to create the file referrer-cleaner.php and put the following code in:<br />
<div class="code"><strong>CODE:</strong><br /><hr /><?php xhtml_highlight('<?<br />
	mail("hide@address.com", "Referrer Script", "<html>\n<head>\n<title>Referrers</title>\n</head>\n<body>\n".file_get_contents(\'referrers.htm\')."\n</body>\n</html>", "From: Referrer Script<hide@address.com>\nMIME-Version: 1.0\nContent-type: text/html; charset=iso-8859-1");<br />
	$fp = fopen("referers.htm","w");<br />
	$string = "";<br />
	fwrite($fp,$string);<br />
	fclose($fp);<br />
?></div><br />
<br />
This is just the code to email you the file, with the email subject "Referrer Script" with the contents being the contents of the referrer log file. The headers part of the mail(); function is extremely important in this scenerio. This tells your email client that it IS an html email. This also clears the referrers log file (referrers.htm) at the same time so the file doesn't get insanely huge.<br />
<br />
"I don't want to have to go to this script EVERYDAY! Are you nuts?!" your prolly screaming. Well, yes I am nuts, but I don't torture people. Plus, I use this script myself, and I'm quite lazy. Let's let our linux host do all the work <img src="/vou-flooder/smileys/wink.gif" alt="Wink" /><br />
<br />
Go to your cPanel, click 'Cron Jobs', and click the 'Standard' button. Fill in in one of the empty slots for a command "GET http://www.yoursite.tld/path/to/referrer-cleaner.php" without the quotes, and set the time for every night at 12:00 AM. This will send you an email every night at midnight with the log file contents. <br />
<br />
Now, you'll notice that I've been using a flat file for all this. Well, open your favorite text editor, save a blank file as referrers.htm, and upload it to your web server. After that, CHMOD the file to at LEAST 666. (Ugh I hate that number)<br />
<br />
Now for that browser script I mentioned.<br />
<br />
<div class="code"><strong>CODE:</strong><br /><hr /><?php xhtml_highlight('<?<br />
$b = $_SERVER[\'HTTP_USER_AGENT\'];<br />
$blist = array (<br />
"Mozilla/4.0|IE 6",<br />
"Mozilla/5.0|Firefox",<br />
"Opera|Opera",<br />
"Mozilla/4.8|Netscape",<br />
"W3C_Validator|Validator",<br />
"Googlebot|GoogleBot",<br />
"msnbot|MSN Bot",<br />
"Jigsaw|CSS Validator"<br />
);<br />
<br />
foreach ($blist as $bnow) {<br />
$bnow = explode ("|", $bnow);<br />
if (eregi ($bnow[0], $b)) {<br />
$endb = $bnow[1];<br />
$check = "No";<br />
} elseif ($check != "No") {<br />
$endb = $_SERVER[\'HTTP_USER_AGENT\'];<br />
}<br />
} <br />
?>'); ?></div><br />
<br />
I'm not really gonna explain this one a whole lot. Its pretty self-explanitory.<br />
<br />
Now to implement all this. <img src="/vou-flooder/smileys/happy.gif" alt="Happy" style="border: none;" /><br />
<br />
It's extremely simple.<br />
<br />
add the following line to any php file that people access. <br />
<div class="code"><strong>CODE:</strong><br /><hr /><?php stripslashes(xhtml_highlight('<? include "referrerlogger.php"; ?>')); ?></div><br />
BOOM, done. Now was that so hard? <br />
<br />
If you have any questions, ask below using the comment system.
Return current item: Modular Site Manager