View Single Post
Old 09-19-2007, 04:40 PM   #1 (permalink)
mortisimus
The Contributor
 
mortisimus's Avatar
 
Join Date: Sep 2007
Location: London, UK
Posts: 47
Thanks: 4
mortisimus is on a distinguished road
Default Log User Data with PHP & MySQL

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:
Code:
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
PHP Code:
<?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_HOSTDB_USERDB_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
PHP Code:
<?php
$page 
$_SERVER['PHP_SELF'];
$ip $_SERVER['REMOTE_ADDR'];
$logq "INSERT INTO logs (page, DATE, ip) VALUES ('$page', NOW(), '$ip')";
$logr = @mysql_query($logq);
?>
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:
PHP Code:
<?php
      $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);
      
      if(
$lr){
      echo 
"<table><th>IP</th><th>Page</th><th>Date</th>";
      while(
$lf mysql_fetch_array($lrMYSQL_ASSOC)){
      echo 
"<tr><td>" $lf['ip'] . "</td><td>" $lf['page'] . "</td><td>" $lf['sd'] . "</td></tr>";
      }
      echo 
"</table>";
      }
      else
      {
      echo 
"No results!";
      }
?>
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.
PHP Code:
<?php
include_once("config.php");
include_once(
"functions.php");
?>
This code includes the config.php file (the one with the database info and connections) and functions.php (the one that actually logs the data).

I hope this helps, if there is any problems or I have missed anything out, just tell me.
mortisimus is offline  
Reply With Quote