To track the visitors coming to our website, we'll use the online.php file.
Each time a visitor loads the page his or her IP address along with a timestamp will
be recorded to our "visitors" array in the online.dat file.
This part is easy. Take a look at the script below for our online.php file:
<?php
|
|
Type the code above into your online.php file and save it. If you run the file, your online.dat file should change. There should now be an entry in the "visitors" array. The key is your IP address, the value is the timestamp when you loaded the page.
The code should be pretty easy to understand. I've commented it to try to give you an idea exactly what's going on.
Lines 11 and 12 grab the current online data from our file and unserialize it so we can work with it. Line 15 is the most important part. This line will update the timestamp for the users browsing your website. You will find out why this is necessary later on.
Because of the way PHP handles arrays, if a new user comes to your website (one that doesn't currenlty have an entry in the visitors array). PHP will automatically create the entry for that user. If the user has been to your website, PHP will simply update the value of that entry.
Now that we have our script adding and updating the visits of our users, we need to deal with "idle" visitors. Because PHP can't execute when a user leaves your website, we'll need to remove idle users ourself.
Continue to the next page to learn how.
| Discuss Tutorial: Visitors Online | 3 Comments |


