PHP Visitor tracker

PHP Visitor tracker

A PHP class to track visitors and logged-in users and show detailed information such as browser, IP, visit time and more.

  • Language: PHP
  • Released: Dec 28, 2011
    Last Update: Dec 27, 2011

Want to show a listing of visitors to your site? With Visitor tracker you can show the current active visitors from your site.

Visitor tracker automatically logs the browser type, username (if set), the page where the visitor is, the IP address, the amount of views and a time stamp of the latest action.

Hide

Change directory permissions

Before we start using this script you need to have write access on the tmp directory. The tmp directory needs to have chmod permission 777. For more information about chmod check this wiki page.

Hide

How to

If you want to use this class first of all you need to start the session handler. We do this by adding the following line.

<?php
// start session
session_start();

After you start a session we can include the class. We do this by adding the following line.

<?php
... rest of the script ...
// include Visitor Class
require 'classes/visitor.class.php';

After you have added the class you can start selecting all active users by using the following line.

<?php
... rest of the script ...
$visitors = Visitor::get_visitors();
?>

This will output something like this if one visitor is active

Array
(
    [0] => Array
        (
            [user] => hehehe
            [page] => /clients/php-visitors/index.php
            [ip] => 127.0.0.1
            [browser] => firefox
            [views] => 29
            [time] => 1324598468
        )

)

If you want to create a table with all the online users, you can do this by adding the following lines.

<table>

        <tr>
                <th class="normal">Browser</th>
                <th class="normal">User (if set)</th>
                <th class="big">Page</th>
                <th class="normal">IP</th>
                <th class="normal">Views</th>
                <th class="big">Last action</th>
        <tr>

        <?php foreach($visitors as $visitor) {?>
        <tr>
                <td><?php echo $visitor['browser'];?></td>
                <td><?php echo htmlspecialchars($visitor['user']);?></td>
                <td><?php echo htmlspecialchars($visitor['page']);?></td>
                <td><?php echo $visitor['ip'];?></td>
                <td><?php echo $visitor['views'];?></td>
                <td><?php echo date('F j, Y, H:i:s', $visitor['time']);?></td>          
        </tr>
        <?php } ?>

</table>

After you selected all active users, you can start logging the visitor. If you place this before Visitor::get_visitors() a inactive user will still be active after a refresh.

<?php
... rest of the script ...
Visitor::track( );

Full example.

<?php 
// start session
session_start();

// include Visitor Class
require 'classes/visitor.class.php';

// get visitors
$visitors = Visitor::get_visitors();

// start tracking the user
Visitor::track( );

?>
<table>

        <tr>
                <th class="normal">Browser</th>
                <th class="normal">User (if set)</th>
                <th class="big">Page</th>
                <th class="normal">IP</th>
                <th class="normal">Views</th>
                <th class="big">Last action</th>
        <tr>

        <?php foreach($visitors as $visitor) {?>
        <tr>
                <td><?php echo $visitor['browser'];?></td>
                <td><?php echo htmlspecialchars($visitor['user']);?></td>
                <td><?php echo htmlspecialchars($visitor['page']);?></td>
                <td><?php echo $visitor['ip'];?></td>
                <td><?php echo $visitor['views'];?></td>
                <td><?php echo date('F j, Y, H:i:s', $visitor['time']);?></td>          
        </tr>
        <?php } ?>

</table>
Hide

Advanced

It's possible to add a username / e-mail address to your sessions. You can do this by adding a parameter at the tracker() method.

<?php
... rest of the script ...
// start tracking the user
Visitor::track( 'Niels' );

Now the user will have the username 'Niels'.

It is also possible to set the session 'time to live'. You can do this by adding the following line.

<?php
... rest of the script ...
Visitor::$ttl = 30;

Now the visitor will be active without any action for 30 minutes.

You need to log-in or create an account
  • Create an account
  • Log-in
Please use your real name.
Activation link will be sent to this address.
Minimum 8 characters
Enter your password again

Clicking this button confirms you read and agreed to the terms of use and privacy policy.

X

Save your watchlist

Fill your details below to receive project updates from your watch list - including new versions, price changes and discounts.

I agree to the terms of use and privacy policy.

2 licenses, starting from From » $4.99 14 day money-back guarantee View Licenses
or Get a quote

for customization or integration services

Post a comment

Or enter your name and Email
No comments have been posted yet.