Suttree.com: Casual Games, Social Software

Read more at the weblog »

PHP Session Counter

This is a basic session counter for display on your home page. A session is defined as an ip address with a referring url of your website (e.g. http://www.suttree.com/) within the last 30 minutes.

Usage
To activate the counter code, add the following few lines of PHP to your homepage.
Note, the counter code expects a variable called to be present, as it uses that as a link indentifier to the MySQL database. Also, since a database connection is required, you must include or set up that connection before including the counter code, as below;

// pull in our include files
include("/path/to/your/database/connection/file/here");

// establish a database connection
$link_id = connect();

// now activate the counter object
include("counter/counter.php");
$counter = new counter();

// Rest of your homepage goes here
That's all there is to it - the line $counter = new counter(); will do everything for you. Next, just add this HTML code to any page you want the counter to appear on;
<script language="JavaScript" src="./counter/counter.js">

...and that's it. Test the code by viewing your homepage and hopefully you should see a line like 'Visitor: 1' appear on your page.

Installation
Decompress the files into a folder called counter in your webroot.
Modify the line that reads 'var $website = "http://yourwebsite.co.uk";' so that yourwebsite.co.uk is replaced with your own URL.
Counter requires a MySQL database in which to store hits. Create a table called counter and one called counter_sessions by executing the following SQL code;

# Table counter
CREATE TABLE counter (
ID int(4) NOT NULL auto_increment,
ip_address varchar(15),
last_active int(25),
PRIMARY KEY (ID)
);
CREATE INDEX counter_total ON counter(ID);

# Table counter_sessions
CREATE TABLE  counter_sessions (
ID int(4) NOT NULL auto_increment,
session_id varchar(100) NOT NULL,
ip_address varchar(15),
referrer varchar(255),
page varchar(255),
last_active int(25),
PRIMARY KEY (ID)
);
CREATE INDEX session_search ON counter_sessions(session_id);

Download
PHP session counter can be downloaded from here;
Download PHP session counter

Version 2
Version 2 isn't planned, but flames, feedback and questions can be directed to me, here.

Suttree Beatniks with better clothing, brought to you by Duncan Gough. About Archives Casual Game Dev Code Disclaimer EcoLocal Music Knows PMOG Tumblee
Links for Saturday, May 17th, 2008