Hello, if any of you guys want to know who is visiting your website and when, then this is for you.
This will consist of 3 files for adding and displaying the logs (plus any other page you wish to log user data from).
The first thing we have to do is create a new table in your database:
CREATE TABLE logs (
id INT(11) NOT NULL AUTO_INCREMENT,
page VARCHAR(50) NOT NULL,
DATE DATETIME NOT NULL,
ip VARCHAR(70) NOT NULL,
PRIMARY KEY (id)
This creates a new table called logs with an auto_increment id (id is automaticly added when ever data is inserted into the table), page, date and ip.
Now we create a config file to connect to your database:
Call it config.php
DEFINE ('DB_USER', 'insert username here');// database username
DEFINE ('DB_PASSWORD', 'insert password here');//database password
DEFINE ('DB_HOST', 'localhost');//database host, usually localhost
DEFINE ('DB_NAME', 'insert database name here');//and finally the database name
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error());
@mysql_select_db (DB_NAME) OR die('Could not select the database: ' . mysql_error() );
Firstly this defines the database info: username, password, host and name. Then it connects to the database using the data provided by you using the mysql_connect function but if it does not connect it will echo the mysql error and stop the script. Finally it selects the database using the DB name you gave, if i can't find the DB it will echo the error and stop page generation with the die function.
Now we will create a functions file: functions.php
This creates a bunch of variables: $page - This uses the $_SERVER['PHP_SELF'] php variable to get the location of the page being logged. $ip - This uses the $_SERVER['REMOTE_ADDR'] php variable to get the users ip. $logq - This is just a simple string that is in the format of a MySQL query, it gets the page, DATE and ip columns from the database and then gets the values to insert: $page, $ip and NOW() (NOW() is a MySQL function that gets the current date in local time) and inserts those values into the database. $logr - This runs the $logq as a proper MySQL query.
Now we need a file to display the logs:
$lq = "SELECT id, ip, page, DATE_FORMAT(date, '%d %M, %Y') as sd FROM logs ORDER BY id DESC LIMIT 50";
$lr = @mysql_query($lq);
All this does is selects data from the logs table in your database and displays the data in a table.
We run a while loop for every time it can get new data from the query. We create a new variable, $lf, to create an associative array of the mysql data using mysql_fetch_array.
Now finally we just need the code to add logs, insert this code into any of your .php or .php3 pages where you want the user data to be logged, Remember to place this code at the top of your page BEFORE ANY html output.
I also find it very handy to log the referrer using:
Its great to see where your visitors come from. Though this will include internal pages. So I run a check to see if my domain is in the $_SERVER['HTTP_REFERER']. If it is, then I won't log it. If it isn't, I log it and then can get a great summary of where most of my visitors are coming from by keeping a count per referring URL. This then becomes useful information when further developing and promoting my sites. :)