Track your visitors, using PHP

Written by Dennis Pallett


There are many different traffic analysis tools, ranging from simple counters to complete traffic analyzers. Although there are some free ones, most of them come with a price tag. Why not do it yourself? With PHP, you can easily create a log file within minutes. In this article I will show you how!

Gettingrepparttar information The most important part is gettingrepparttar 105083 information from your visitor. Thankfully, this is extremely easy to do in PHP (or any other scripting language for that matter). PHP has a special global variable called $_SERVER which contains several environment variables, including information about your visitor. To get allrepparttar 105084 information you want, simply userepparttar 105085 following code:

 // Gettingrepparttar 105086 information $ipaddress = $_SERVER['REMOTE_ADDR']; $page = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}";  $page .= iif(!empty($_SERVER['QUERY_STRING']), "?{$_SERVER['QUERY_STRING']}", ""); $referrer = $_SERVER['HTTP_REFERER']; $datetime = mktime(); $useragent = $_SERVER['HTTP_USER_AGENT']; $remotehost = @getHostByAddr($ipaddress); 
As you can seerepparttar 105087 majority of information comes fromrepparttar 105088 $_SERVER variable. The mktime() (http://nl2.php.net/mktime) and getHostByAddr() (http://nl2.php.net/manual/en/function.gethostbyaddr.php) functions are used to get additional information aboutrepparttar 105089 visitor.

Note: I used a function inrepparttar 105090 above example called iif(). You can get this function at http://www.phpit.net/code/iif-function.

Loggingrepparttar 105091 information Now that you have allrepparttar 105092 information you need, it must be written to a log file so you can later look at it, and create useful graphs and charts. To do this you need a few simple PHP function, like fopen (http://www.php.net/fopen) and fwrite (http://www.php.net/fwrite).

The below code will first create a complete line out of allrepparttar 105093 information. Then it will openrepparttar 105094 log file in "Append" mode, and if it doesn't exist yet, create it.

If no errors have occurred, it will writerepparttar 105095 new logline torepparttar 105096 log file, atrepparttar 105097 bottom, and finally closerepparttar 105098 log file again.

 // Create log line $logline = $ipaddress . '|' . $referrer . '|' . $datetime . '|' . $useragent . '|' . $remotehost . '|' . $page . " ";

// Write to log file: $logfile = '/some/path/to/your/logfile.txt';

// Openrepparttar 105099 log file in "Append" mode if (!$handle = fopen($logfile, 'a+')) { die("Failed to open log file"); }

// Write $logline to our logfile. if (fwrite($handle, $logline) === FALSE) { die("Failed to write to log file"); } fclose($handle);

Now you've got a fully function logging module. To start tracking visitors on your website simply includerepparttar 105100 logging module into your pages withrepparttar 105101 include() function (http://www.php.net/include):
 include ('log.php'); 
Okay, now I want to view my log file After a while you'll probably want to view your log file. You can easily do so by simply using a standard text editor (like Notepad on Windows) to openrepparttar 105102 log file, but this is far from desired, because it's in a hard-to-read format.

PHP On-The-Fly!

Written by Dennis Pallett


Introduction PHP can be used for a lot of different things, and is one ofrepparttar most powerful scripting languages available onrepparttar 105081 web. Not to mention it's extremely cheap and widely used. However, one thing that PHP is lacking, and in fact most scripting languages are, is a way to update pages in real-time, without having to reload a page or submit a form.

The internet wasn't made for this. The web browser closesrepparttar 105082 connection withrepparttar 105083 web server as soon as it has received allrepparttar 105084 data. This means that after this no more data can be exchanged. What if you want to do an update though? If you're building a PHP application (e.g. a high-quality content management system), then it'd be ideal if it worked almost like a native Windows/Linux application.

But that requires real-time updates. Something that isn't possible, or so you would think. A good example of an application that works in (almost) real-time is Google's GMail. Everything is JavaScript powered, and it's very powerful and dynamic. In fact, this is one ofrepparttar 105085 biggest selling-points of GMail. What if you could have this in your own PHP websites as well? Guess what, I'm going to show you in this article.

How does it work? If you want to execute a PHP script, you need to reload a page, submit a form, or something similar. Basically, a new connection torepparttar 105086 server needs to be opened, and this means thatrepparttar 105087 browser goes to a new page, losingrepparttar 105088 previous page. For a long while now, web developers have been using tricks to get around this, like using a 1x1 iframe, where a new PHP page is loaded, but this is far from ideal.

Now, there is a new way of executing a PHP script without having to reloadrepparttar 105089 page. The basis behind this new way is a JavaScript component calledrepparttar 105090 XML HTTP Request Object. See http://jibbering.com/2002/4/httprequest.html for more information aboutrepparttar 105091 component. It is supported in all major browsers (Internet Explorer 5.5+, Safari, Mozilla/Firefox and Opera 7.6+).

With this object and some custom JavaScript functions, you can create some rather impressive PHP applications. Let's look at a first example, which dynamically updatesrepparttar 105092 date/time.

Example 1 First, copyrepparttar 105093 code below and save it in a file called 'script.js':

 var xmlhttp=false; /*@cc_on @*/ /*@if (@_jscript_version >= 5) // JScript gives us Conditional compilation, we can cope with old IE versions. // and security blocked creation ofrepparttar 105094 objects.  try {  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");  } catch (e) {  try {  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");  } catch (E) {  xmlhttp = false;  }  } @end @*/ if (!xmlhttp && typeof XMLHttpRequest!='undefined') {  xmlhttp = new XMLHttpRequest(); }

function loadFragmentInToElement(fragment_url, element_id) { var element = document.getElementById(element_id); element.innerHTML = '<em>Loading ...</em>'; xmlhttp.open("GET", fragment_url); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { element.innerHTML = xmlhttp.responseText; } } xmlhttp.send(null); }

Then copyrepparttar 105095 code below, and paste it in a file called 'server1.php':
 <?php echo date("l dS of F Y h:i:s A"); ?> 
And finally, copyrepparttar 105096 code below, and paste it in a file called 'client1.php'. Please note though that you need to editrepparttar 105097 line that says 'http://www.yourdomain.com/server1.php' torepparttar 105098 correct location of server1.php on your server.
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN"> <html> <head> <title>Example 1</title> <script src="script.js" type="text/javascript"></script>

<script type="text/javascript"> function updatedate() { loadFragmentInToElement('http://www.yourdomain.com/server1.php', 'currentdate'); }

</script> </head>

<body> The current date is<span id="currentdate"><?php echo date("l dS of F Y h:i:s A"); ?></span>.<br /><br />

<input type="button" value="Update date" OnClick="updatedate();" /> </body>

Cont'd on page 2 ==>
 
ImproveHomeLife.com © 2005
Terms of Use