problem regarding cookies.
Another questoin after two answered questions. Okkk! This is the problem regarding cookies. where i am creating a mysql table and storing the information of each visit by the browsers. if browser has visited the site. it will treat that as old user. else it will insert the information of new user in database. But the problem is: " Every time it is creating a new row in table and treating each attempt as a new attempt." Can any one help me about this? I am sending the format of the database also. Try it............
create table track_visit (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY( id ),
first_visit INT,
last_visit INT,
num_visits INT,
total_duration INT,
total_clicks INT
);
************************************************** ************************************************** ****************
<?php
$link = mysql_connect( "localhost", "root", "root" );
if ( ! $link )
die( "Couldn't connect to mysqld" );
mysql_select_db( "test", $link ) or die ( mysql_error() );
if ( ! isset ( $visit_id ) ) {
$user_stats = newuser( $link );
}
else {
$user_stats = olduser( $link, $visit_id, $slength );
print "Welcome back $visit_id<P>";
}
function newuser( $link )
{
// a new user!
$visit_data = array (
first_visit => time(),
last_visit => time(),
num_visits => 1,
total_duration => 0,
total_clicks => 1
);
// $visit_data[id] = mysql_insert_id();
// setcookie( "visit_id", $visit_data[id], time()+(60*60));
$query = "INSERT INTO track_visit ( first_visit,
last_visit,
num_visit,
total_duration,
total_clicks ) ";
$query .= "values( $visit_data[first_visit],
$visit_data[last_visit],
$visit_data[num_visits],
$visit_data[total_duration],
$visit_data[total_clicks] )";
$result = mysql_query( $query );
$visit_data[id] = mysql_insert_id();
setcookie( "visit_id", $visit_data[id], time()+(60*60),"/");
echo "<script type=text/javascript>window.alert(\"Finish of the line.\")</script>";
return $visit_data;
}
function olduser( $link, $visit_id, $slength )
{
// s/he's been here before!
$query = "SELECT * FROM track_visit WHERE id=$visit_id";
$result = mysql_query( $query );
if ( ! mysql_num_rows( $result ) )
// cookie found but no id in database -- treat as new user
return newuser( $link );
$visit_data = mysql_fetch_array( $result, $link );
// there has been another hit so increment
$visit_data[total_clicks]++;
if ( ( $visit_data[last_visit] + $slength ) > time() )
// still in session so add to total elapsed time
$visit_data[total_duration] +=
( time() − $visit_data[last_visit] );
else
// this is a new visit
$visit_data[num_visits]++;
// update the database
$query = "UPDATE track_visit SET last_visit=".time().",
num_visits=$visit_data[num_visits], ";
$query .= "total_duration=$visit_data[total_duration],
total_clicks=$visit_data[total_clicks] ";
$query .= "WHERE id=$visit_id";
$result = mysql_query( $query );
return $visit_data;
}
?>
************************************************** ************************************************** ****************
Another problem. Can i shift my default cookies address in windows? If it is possible then what is method or steps to change the default cookies address?
Keep in mind, i am saying about windows invironment.
Waiting for reply.......
Another roblem is not able to set the duration time in table.
|