Home
1 site, unlimited servers No source distribution Commercial use allowed Can modify source Read full license | More Info
Unlimited projects Source and binary distribution Commercial use allowed Distribute modifications 6 months support Read full license | More Info
Starting from $ 4.99
Integration time
Saves you
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.
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.
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>
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.
Starting from $ 4.99
Questions & Comments
Leave a comment
Log-in now or register for a free account.