PHP Visitor tracker

PHP Visitor tracker

Released 3 years ago , Last update 3 years ago

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

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.

Pricing

$4.99

Personal License

  • Perpetual license

  • 1 site, unlimited servers

  • No distribution (hosted use only)

  • Commercial use

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.

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>

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.

2 licenses, starting from From » $4.99 View Licenses

Get A Quote

What do you need?
  • Custom development
  • Integration
  • Customization / Reskinning
  • Consultation
When do you need it?
  • Soon
  • Next week
  • Next month
  • Anytime

Thanks for getting in touch!

Your quote details have been received and we'll get back to you soon.


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