See more articles about "Websites Help "

Clue Your Visitors, Using PHP



 20 November 00: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 logline to the log file, at the bottom, and assuredly abutting the log book again.

    <pre>

    // Make log line

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

    // Address to log file:

    $logfile = /some/path/to/your/logfile.txt ;

    // Accessible the log book in "Append" mode

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

     die("Failed to accessible log file");

    }

    // Address $logline to our logfile.

    if (fwrite($handle, $logline) === FALSE) {

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

    }

    fclose($handle);

    </pre>

    Now you ve got a absolutely action logging module. To alpha tracking visitors on your website artlessly cover the logging 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

    $logfile = "/some/path/to/your/logfile.txt";

    if (file_exists($logfile)) {

    

     $handle = fopen($logfile, "r");

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

     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 logline is in a separate variable. We can do this using the explode() action ( php.net/explode ), like so:

    <pre>

    // Seperate anniversary logline

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

    </pre>

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

    <pre>

    // Seperate anniversary allotment in anniversary logline

    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 logfile

    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 $logline) {

     echo <tr> ;

     echo <td> . $logline . </td> ;

     echo <td> . urldecode($logline) . </td> ;

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

     echo <td> . $logline . </td> ;

     echo <td> . $logline . </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 logging 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 .

    


 

TAG: logline, logfile, server, function, create, information, handle, variable, explode, logging, write, complete, count, getting, simply,
  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 ...

How Video Can Be Acclimated Finer Online
How Video Can Be Acclimated Finer Online A able way to back your advice letters to your admirers is to be able to accept your promotional video accessible on your website. Yet, we accept apparently all accomplished visiting a website and beat on a video alone to accept to been af

How To Get A Website Online And Accept It Authoritative Money
How To Get A Website Online And Accept It Authoritative Money When architecture and accepting a website online you accept to anticipate of a amount of things. Some of these cover the following:1. What is your website traveling to be aboutIf you wish to get a website online to

How to acquisition acceptable Web Hosting for your site!
How to acquisition acceptable Web Hosting for your site! When architecture your first website the capital things that you will apparently anticipate about is what types of agreeable you will be abacus to your site, how you are traveling to get visitors to your website and how you are

Cerebration About Alteration to a New Web Hosting Company? 5 Accomplish to Plan Advanced for the Move
Thinking About Alteration to a New Web Hosting Company? 5 Accomplish to Plan Advanced for the Move Perhaps your web hosting aggregation has a abstruse abutment accumulation that never answers their email, or their ascendancy console is difficult to use. Maybe your website has been ac

Are your SEO efforts traveling to waste?
Are your SEO efforts traveling to waste? Search engine enhancement (SEO) is a continued and complicated action that can be awful advantageous if done correctly. SEO is not a decay of time, but can be if your website doesnt address to visitors or action properly. Your abeyant chump wi

Youve Got a New Website, But How Do You Get Association to Attending at it?
You ve Got a New Website, But How Do You Get Association to Attending at it? I took a attending at a new Website afresh in acknowledgment to a appeal for a website analysis on one of my admired forums. And, while the actualization was pleasing, there wasn t absolutely abundant there.

Now you accept a Web site. Accept you anytime heard of accessibility?
Now you accept a Web site. Accept you anytime heard of accessibility? An attainable Web website is calmly approached, calmly understood, and adapted for all. There are accessibility standards set alternating by the Apple Advanced Web Consortium, which all sites should attach to as ab

Allotment the appropriate web host
Choosing the appropriate web host Whatever blazon of website you wish to host, allotment the actual host can be tricky. Alotof hosting companies action you added than you will anytime use, their sales agents acclaim high end bales for baby websites. Be accurate if choosing, you w

PHP and Cookies; a Acceptable Mix!
PHP and Cookies; a Acceptable Mix! IntroductionCookies accept continued been acclimated in PHP scripts, and are a actual advantageous function. But what absolutely are cookies? Maybe you accept acclimated then, but you still don t understand absolutely what they are. Or you are a

Accessible Antecedent Chargeless Software De-Mystified
Open Antecedent Chargeless Software De Mystified Did you understand that the alotof able software and web applications are all free? Yes that s right, just about every bartering software or web appliance you understand of is alotof acceptable accessible as a chargeless Accessible An