PHP Session for Tracking Unique Page Views

PHP session for tracking unique page views

Problems:

  1. You are updating in tblCount EACH time, because your session is closed each time your script finishes.
    SO: Put the session_start()call as the FIRST LINE in code.
  2. It's not permitted to set an integer as $_SESSION variable. So if you set $_SESSION[$pagenumber] = 'something', then you gain the following notice:

( ! ) Notice: Unknown: Skipping numeric key 1 in Unknown on line 0

Quite... not understandable. For details see this answer.

Solution:

Add your $pagenumber as index in an array (here pagenumbers) and that array inside the $_SESSION variable. No notice anymore.

session_start();

$pagenumber = 1;

if (!isset($_SESSION['pagenumbers'])) {
$_SESSION['pagenumbers'] = array();
}

if (!isset($_SESSION['pagenumbers'][$pagenumber])) {
updateViews($pagenumber);
$_SESSION['pagenumbers'][$pagenumber] = $pagenumber;
}

echo 'Page number: ' . $_SESSION['pagenumbers'][$pagenumber] . '<br/>';
$views = getViews($pagenumber);
echo '<pre>Viewed ' . print_r($views, true) . ' times</pre>';

Note: I used my functions to test. They just replace your db-processing code.

Tracking unique visitors only?

The simplest method would be cookie checking.

A better way would be to create an SQL database and assign the IP address as the primary key. Then whenever a user visits, you would insert that IP into the database.

  1. Create a function included on all pages that checks for $_SESSION['logged'] which you can assign whatever 'flag' you want.
  2. If $_SESSION['logged'] returns 'false' then insert their IP address into the MySQL database.
  3. Set $_SESSION['logged'] to 'true' so you don't waste resources logging the IP multiple times.

Note: When creating the MySQL table, assign the IP address' field as the key.

<?php 
session_start();
if (!$_SESSION['status']) {
$connection = mysql_connect("localhost", "user", "password");
mysql_select_db("ip_log", $connection);

$ip = $_SERVER['REMOTE_ADDR'];
mysql_query("INSERT INTO `database`.`table` (IP) VALUES ('$ip')");

mysql_close($connection);
$_SESSION['status'] = true;
}
?>

Easy way to check if a page view is unique?

You could store if the client has already visited the page in a session variable, and run the same views query. Now this won't prevent a user from closing the browser, reopening, and returning, but it should provide a decent level of protection. If that's not enough you can move to cookies, followed by user registration, or IP tracking.

Okay, the session approach. On your page you run:

session_start();
if(!isset($_SESSION['page_views']['some_unique_string'])){
$_SESSION['page_views']['some_unique_string'] = true;
// update your database
}

Note that you'll want to make sure 'some_unique_string' is unique to the page you're tracking views on.

The approach is nearly identical for cookies.

Track Unique Visitors with PHP-GA Server Side Analytics Tracking

After searching for a while on how to solve this, all that appears to be needed is to start the session. Normally this is a given, but it might be forgotten when using on an external system.

session_start();

How to track page views

You can track them in a database if you're rolling your own. Every time a page loads, you call a method that will decide whether or not to up the page views. You can add whatever criteria you like.

IF IP is unique

OR IP hasn't visited in 20 minutes based on a session variable

ETC

THEN add a page view record

| ID | IPAddress | ViewDateTime |
| 1 | 1.2.3.4 | Oct 18 ... |

However, session variables can get pretty load intensive on sites with as many visitors as SO. You might have to get a little more creative.

Now, if you don't want to code it, the I would suggest looking into SmarterStats as it reads your server logs and is more robust than analytics.

note: i'm not sure about a similar Apache software



Related Topics



Leave a reply



Submit