Personal License $4.99

1 site, unlimited servers No source distribution Commercial use allowed Can modify source Read full license | More Info

Developer License $59.99

Unlimited projects Source and binary distribution Commercial use allowed Distribute modifications 6 months support Read full license | More Info

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

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

Starting from $ 4.99

View Pricing 14 days money-back guarantee

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.

Share and win up to $15k in prizes each month!Learn more

Share

Description

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.

Back to top

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.

Back to top

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>
Back to top

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.

Questions & Comments

Leave a comment

You must be logged-in to leave a comment.
Log-in now or register for a free account.
No comments have been posted yet.
You must be logged-in to vote. Log-in to your account or register now.

User Reviews

No reviews have been submitted yet.

Starting from $ 4.99

View Pricing 14 days money-back guarantee