Counting Visitors

While we're on the subject of the Session Object, let's take a look at a routine that takes good advantage of its functionality--counting visitors. The Session Object actual predefines a "visitor" by virtue of the fact that it maintains a session for each person that lasts for 20 minutes after that person's last access to a page. Thus, the mere existence of a session is evidence of a visitor. So, to count visitors, all we need do is to add 1 to a visitor counter whenever a session is started.

Establishing a Visitor Counter

First, we need to establish a visitor counter and initialize its value. Normally, you would set the counter to zero; but, if you want to impress your visitors or your investors you could initialize it to 1,000,000. The counter could be a simple text file containing the counter value. Since we're working in a database environment, it would be just as well to create a table. There's not much in the table, just a single record with a single field in which the visitor count is incremented.

Here we've created a table named Counters (since we might think of some additional counters we'll need later). The table contains a single field named VisitorCounter defined as a number field. There is only this single record in the table. Now we're ready to count visitors.

Incrementing the Visitor Counter

Whenever a visitor arrives at our site, we need to add 1 to the visitor counter. We know that a visitor arrives when PHP establishes a session and creates a Session Object for this person. So, we will create a global session variable named "Count" $_SESSION[Count] and add 1 to variable each time a new session is started. We will use this value to increment the VisitorCounter. To prevent counting the same user during the same session, we can place the counter update code inside of an if statement that checks to see if Count has already been assigned a value. If Count is empty, this indicates a new session or a new visitor and the condition is met to update the Visitor counter. If Count is not empty, the current user session has already been accounted for and the update block is ignored.

Since home.php is the "entrance" to our site, let's return to our home.php file and add the necessary code to count visitors:

  session_start();

   if (!$_SESSION[Count])

   {
   $_SESSION[Count] = 1;
   $conn = odbc_connect('Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\inetpub\wwwroot\PHPTutorial\Ecommerce\databases\ecommerce.mdb','','');
   $sql = "UPDATE Counters SET VisitorCounter = VisitorCounter + $_SESSION[Count]";
   $rs = odbc_exec($conn, $sql)
   odbc_close($conn);

   }

We link to and open the Counters table and add 1 to the VisitorCounter field. This action takes place every time someone new arrives at our site. NOTE:Don't forget to issue a call to the session_start() function prior to checking the status of the session Count variable. If a session is not started, $_SESSION[Count] will not be recognized as a session variable and the if statement will be executed each time the page is reloaded. In other words, the counter will increment each time the page is refreshed and not each time a new session is created.

Using the SQL UPDATE Statement

The SQL UPDATE statement used above has the following general format:

UPDATE TableName SET FieldName1 = expression1 [,FieldName2 = expression2] ...

where TableName is the name of the table and FieldName refers to the table field to which the update applies. The expressions can be a single value or can be an arithmetic expression to generate a value. A WHERE clause can be appended to the statement to restrict updates to particular records matching the given criteria.

Displaying the Visitor Counter

The visitor count is displayed on our home.php page. Wherever you intend for it to appear, you'll use the following type of script to read and display the value:

<?php
$conn = odbc_connect
  ('Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\inetpub\wwwroot\PHPTutorial\Ecommerce\databases\ecommerce.mdb','','');
$sql = "SELECT VisitorCounter FROM Counters";
$rs = odbc_exec($conn, $sql);
$Counts = odbc_result($rs,VisitorCounter);
echo "You are visitor $Counts since 12/2006";
?>

When you test these counter routines on your pages remember one important fact. You will not get the counter to increment simply by refreshing the page. The counter increments only on the arrival of a new visitor and the creation of a new session. Therefore, you will need to close down your browser (ending your current session) and reopen it (starting a new session) to increment the counter.