If you've always wanted to learn how to display the number of visitors browsing your website, look no further, this tutorial will teach you all you need to know.
As usual, this tutorial will make use of a flat file database rather than a MySQL database. If you haven't already read the tutorial covering flat files and you don't have prior experience with them, go ahead and do that before continuing here. It will make things a bit easier.
The first thing we need to do is setup our php and data storage files. Create two new files. Name the first file online.php, name the second online.dat. Make sure you CHMOD the online.dat file to 777 or 755 so that PHP is able to write information to it.
Now that we have both of our files created, we're going to need to setup a "skeleton" array for our online.dat file. Basically, this is the planning stages for our database. This array is where we will store information about who is online.
Our skeleton array will be multidimensional and look something like this:
Array
|
|
The visitors array is empty for now. It will be filled with data about each new visitor that accesses your website in the form of a key=>value pair. The key will be the visitor's IP address, the value will be a timestamp of his or her most recent pageview.
Now that we know what our skeleton array will need to look like, lets create it an stick it into our file. To do this I'm going to create a third file, we'll be able to delete this file later. Create a file named setup.php.
Type the following for your setup.php file:
<?php
|
|
After you run this php file (type its location in your address bar). Your online.dat file should look like the example below. Note that the contents of your online.dat file should be on one line. The example shown below was wrapped to two lines for design purposes.
a:2:{s:8:"visitors";a:0:{}s:6:"record";
|
|
After you've played around with flat files and serialization for a while, you'll be able to write serialized arrays without the use of a setup.php file. For now, though, using PHP to create it for us will save us some frustration.
Now that we have our skeleton array written to our online.dat file we can begin feeding data into it. Continue to the next page to learn how to do this.
| Discuss Tutorial: Visitors Online | 3 Comments |


