TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-19-2007, 03:40 PM   #1 (permalink)
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
Old 09-19-2007, 05:02 PM   #2 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Very handy for statistical purposes. :D
Haris is offline  
Reply With Quote
Old 09-21-2007, 11:21 PM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Superb script. Thanks a lot for sharing! What we need next is to check for unique visitors and log those. Now that would be a nice addition :)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 09-22-2007, 02:05 AM   #4 (permalink)
The Wanderer
 
Hashimi's Avatar
 
Join Date: Sep 2007
Posts: 10
Thanks: 2
Hashimi is on a distinguished road
Default

Thanks for this nice Tutorial; i really like your kind of explanation. Keep On..
Hashimi is offline  
Reply With Quote
Old 09-22-2007, 05:43 PM   #5 (permalink)
The Contributor
 
mortisimus's Avatar
 
Join Date: Sep 2007
Location: London, UK
Posts: 47
Thanks: 4
mortisimus is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
Superb script. Thanks a lot for sharing! What we need next is to check for unique visitors and log those. Now that would be a nice addition :)
kk, working on it...
mortisimus is offline  
Reply With Quote
Old 09-22-2007, 07:34 PM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Good man. I suppose you could also apply the X_FORWARDED_FOR header attribute into it as well if you wanted to be really clever. See this article.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 09-30-2007, 05:52 PM   #7 (permalink)
The Wanderer
 
Join Date: Sep 2007
Location: Sydney, Australia
Posts: 19
Thanks: 0
jordie is on a distinguished road
Default

I also find it very handy to log the referrer using:

PHP Code:
$_SERVER['HTTP_REFERER'
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. :)
jordie is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 12:21 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design