Forum Moderators: coopster

Message Too Old, No Replies

storing IP in mysql database

referrer and IP storing

         

benevolent001

12:28 pm on Apr 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I want to write php script along with mysql database to do the following

It should get the following information about the user and store them in the mysql database

User IP
Referrer
Landing Page
Pages visited during visit
Number of visits during the day
Time spent on visit

How this can be done,any hint in this regard will be highly appreciated
Please also let me know if there are some already made scripts for doing the same

lobo235

8:09 pm on Apr 2, 2005 (gmt 0)

10+ Year Member



I would use PHP's built in session functions to do this. You will want to start a session and then use that session to track each user. This is usually the best way to do this because sometimes you will get multiple visitors from the same IP. For example, most companies have a gateway/proxy that all traffic passes through so to your website it looks like it's the same IP/person.

Save the session_id() into the database and each time someone visits the page you have to check their session_id() and see if it matches an ID in the database. If it does then update their info (number of pages viewed this visit, total daily page views, etc.) otherwise put in a new entry. There is a good amount of coding to accomplish this but you can find all the information you need to do it out on the web.

Hope this helps!

rubenski

12:01 am on Apr 3, 2005 (gmt 0)

10+ Year Member



Here is a stripped down version of my own stats script:

$ip = $REMOTE_ADDR;

if ($var_ip!="your_own_ip_here_if_you don't_want_tol_log_your_own_pageviews")
{
$date = date("Y/m/d");
$time = date ("h:i:s A");
$agent= $HTTP_USER_AGENT;
$referrer = $HTTP_REFERER;

$host="db_host_probably_localhost";
$user="db_username";
$password="db_password";
$dbname="db_name";

$connect=mysql_connect($host,$user,$password);
mysql_select_db($dbname, $connect);

$sql="INSERT INTO pageviews (date, time, agent, ip, referrer) VALUES ('".$date."', '".$time."','".$agent."','".$ip."','".$referrer."')";
mysql_query($sql);
echo mysql_errno() . "error: " . mysql_error();
mysql_close();
}

This simply writes the info to the database every time the page is requested. To find out advanced stuff like 'time spent', 'landing page', etc, you can't do without sessions.