Clue Your Visitors, Using PHP

 31 December 18:00   Track Your Visitors, Using PHP

     There are some altered cartage assay tools, alignment from simple counters to complete cartage analyzers. Although there are some chargeless ones, alotof of them appear with a amount tag. Why not do it yourself? With PHP, you can calmly make a log book aural minutes. In this commodity I will appearance you how!

    Getting the information

    The alotof important allotment is accepting the advice from your visitor. Thankfully, this is acutely simple to do in PHP (or any additional scripting accent for that matter). PHP has a appropriate all-around capricious alleged $_SERVER which contains several ambiance variables, including advice about your visitor. To get all the advice you want, artlessly use the afterward code:

    <pre>

    // Accepting the information

    $ipaddress = $_SERVER;

    $page = "{$_SERVER}{$_SERVER}";

    $page .= iif(!empty($_SERVER), "?{$_SERVER}", "");

    $referrer = $_SERVER;

    $datetime = mktime();

    $useragent = $_SERVER;

    $remotehost = @getHostByAddr($ipaddress);

    </pre>

    As you can see the majority of advice comes from the $_SERVER variable. The mktime() ( nl2.php.net/mktime ) and getHostByAddr() ( nl2.php.net/manual/en/function.gethostbyaddr.php ) functions are acclimated to get added advice about the visitor.

    Note: I acclimated a action in the aloft archetype alleged iif(). You can get this action at phpit.net/code/iif-function .

    Logging the information

    Now that you accept all the advice you need, it haveto be accounting to a log book so you can after attending at it, and make advantageous graphs and charts. To do this you charge a few simple PHP function, like fopen ( php.net/fopen ) and fwrite ( php.net/fwrite ).

    The beneath cipher will first make a complete band out of all the information. Then it will accessible the log book in "Append" mode, and if it doesn t is yet, make it.

    If no errors accept occurred, it will address the new log line to the log file, at the bottom, and assuredly abutting the log book again.

    <pre>

    // Make log line

    $ log line = $ipaddress . | . $referrer . | . $datetime . | . $useragent . | . $remotehost . | . $page . "n";

    // Address to log file:

    $ log file = /some/path/to/your/ log file.txt ;

    // Accessible the log book in "Append" mode

    if (!$handle = fopen($ log file, a+ )) {

     die("Failed to accessible log file");

    }

    // Address $ log line to our log file.

    if (fwrite($handle, $ log line) === FALSE) {

     die("Failed to address to log file");

    }

    fclose($handle);

    </pre>

    Now you ve got a absolutely action log ging module. To alpha tracking visitors on your website artlessly cover the log ging bore into your pages with the include() action ( php.net/include ):

    <pre>

    include ( log .php );

    </pre>

    Okay, now I wish to appearance my log file

    After a while you ll apparently wish to appearance your log file. You can calmly do so by artlessly using a accepted argument editor (like Block on Windows) to accessible the log file, but this is far from desired, because it s in a hard-to-read format.

    Let s use PHP to accomplish advantageous overviews for is. The first affair that needs to be done is get the capacity from the log book in a variable, like so:

    <pre>

    // Accessible log file

    $ log file = "/some/path/to/your/ log file.txt";

    if (file_exists($ log file)) {

    

     $handle = fopen($ log file, "r");

     $ log = fread($handle, filesize($ log file));

     fclose($handle);

    } abroad {

     die ("The log book doesn t exist!");

    }

    </pre>

    Now that the log book is in a variable, it s best if anniversary log line is in a separate variable. We can do this using the explode() action ( php.net/explode ), like so:

    <pre>

    // Seperate anniversary log line

    $ log = explode("n", trim($ log ));

    </pre>

    After that it may be advantageous to get anniversary allotment of anniversary log line in a separate variable. This can be done by looping through anniversary log line, and using backfire again:

    <pre>

    // Seperate anniversary allotment in anniversary log line

    for ($i = 0; $i < count($ log ); $i++) {

     $ log = trim($ log );

     $ log = explode( | , $ log );

    }

    </pre>

    Now the complete log book has been parsed, and we re accessible to alpha breeding some absorbing stuff.

    The first affair that is actual simple to do is accepting the amount of pageviews. Artlessly use count() ( phpit.net/count ) on the $ log array, and there you accept it;

    <pre>

    echo count($ log ) . " humans accept visited this website.";

    </pre>

    You can aswell accomplish a complete overview of your log file, using a simple foreach bend and tables. For example:

    <pre>

    // Appearance a table of the log file

    echo <table> ;

    echo <th>IP Address</th> ;

    echo <th>Referrer</th> ;

    echo <th>Date</th> ;

    echo <th>Useragent</th> ;

    echo <th>Remote Host</th> ;

    foreach ($ log as $ log line) {

     echo <tr> ;

     echo <td> . $ log line . </td> ;

     echo <td> . urldecode($ log line) . </td> ;

     echo <td> . date( d/m/Y , $ log line) . </td> ;

     echo <td> . $ log line . </td> ;

     echo <td> . $ log line . </td> ;

     echo </tr> ;

    }

    echo </table> ;

    </pre>

    You can aswell use custom functions to clarify out seek engines and crawlers. Or make graphs using PHP/SWF Archive ( maani.us/charts/index.php ). The possibilities are endless, and you can do all kinds of things!

    In Conclusion...

    In this commodity I accept apparent you accept to make a log ging bore for your own PHP website, using annihilation added than PHP and its congenital functions. To appearance the log book you charge to anatomize it using PHP, and then affectation it in whatever way you like. It is up to you to make a kick-ass cartage analyzer.

    If you still adopt to use a pre-built cartage analyzer, accept a attending at hotscripts.com .

    

 


Tags: create, website, simple, getting, visitors, complete, information, server, traffic, handle, functions, write, visitor, charts, string

 pre>, ", logline, th>, td>, logfile, server, function, create, information, handle, variable, explode, logging, fopen, write, complete, count, getting, simply, visitors, traffic, website, module, simple, charts, mktime, ipaddress, track, functions, visitor, useful, fwrite, , log file, < pre>, echo <, < td>, php net, td> echo, < th>, th> echo, getting the, td> logline, open the, visitors using, server http, track your, pre> now, logging module, < td> echo, track your visitors, < td> logline, < pre> seperate, pre> seperate each, handle fopen logfile, php track your, server query string, log file logfile,

Share Clue Your Visitors, Using PHP: Digg it!   Google Bookmarks   Del.icio.us   Yahoo! MyWeb   Furl  Binklist   Reddit!   Stumble Upon   Technorati   Windows Live   Bookmark

Text link code :
Hyper link code:

Also see ...

Permalink
Article In : Computers & Technology  -  Websites Help