Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 2nd, 2009, 10:58 AM
Registered User
 
Join Date: Sep 2009
Posts: 6
Thanks: 1
Thanked 1 Time in 1 Post
Post 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.
Reply With Quote
  #2 (permalink)  
Old December 13th, 2009, 10:04 PM
Authorized User
 
Join Date: Dec 2008
Location: London
Posts: 50
Thanks: 1
Thanked 5 Times in 5 Posts
Default

OK, you are using ID field in your database to identify unique users which is wrong because after each insert a new one will be created.

rather use

$id = session_id();

using session_id() the id will remain the same until the browser is closed.

Kind regards.
Reply With Quote
  #3 (permalink)  
Old December 13th, 2009, 10:11 PM
Authorized User
 
Join Date: Dec 2008
Location: London
Posts: 50
Thanks: 1
Thanked 5 Times in 5 Posts
Default

found you are not reading the cookie value in the else clause

see this page http://php.net/manual/en/reserved.variables.cookies.php

and you need to read the $visit_id from variable $_COOKIES['visit_id']

hope that helps.

kind regards.
Reply With Quote
Reply


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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem regarding cookies. anup_prakash36 Beginning PHP 0 October 2nd, 2009 10:45 AM
Problem with cookies khb3283 ASP.NET 2.0 Basics 1 October 1st, 2008 11:11 AM
cookies problem kanoorani Servlets 0 December 22nd, 2006 10:54 AM
Problem with cookies :( Varg_88 Classic ASP Databases 2 December 13th, 2004 11:34 AM



All times are GMT -4. The time now is 05:51 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.