Re: tracking users...

From: Date: Wed, 23 Jan 2002 08:57:08 +0000
Subject: Re: tracking users...
References: 1 2  Groups: php.db 
Request: Send a blank email to php-db+get-16065@lists.php.net to get a copy of this message
Well I think I have a solution to my original problem which does involve using a MySQL db to store the necessary information. For those that would like to use this script please do because it works excellent. Here is the table info. for MySQL... CREATE TABLE sessions ( session varchar(50) NOT NULL, site varchar(60) NOT NULL, referrer varchar(150) NOT NULL, remote_host varchar(30) NOT NULL, user_agent varchar(60) NOT NULL, starttime datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, endtime datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, pagecount int(4) DEFAULT '0' NOT NULL, pages text NOT NULL, referrer_string text NOT NULL, PRIMARY KEY (session), UNIQUE session (session), KEY site (site), KEY referrer (referrer) ); Once you have the table created all you have to do is stick this script at the top of any page you want tracked, given the page must have an extenstion of .php duh. <? $var['dbServer'] = 'your_database_server'; $var['dbDatabase'] = 'your_database_name'; $var['dbUser'] = 'your_database_user_name'; $var['dbPass'] = 'your_database_password'; $var['SessionsTable'] = 'sessions'; @mysql_pconnect($var['dbServer'],$var['dbUser'],$var['dbPass']); $var['Timestamp'] = time(); $var['Datetime'] = date("Y-m-d H:i:s"); if(isset($varSessionID)) $var['SessionID'] = $varSessionID; if(!isset($var[SessionID])){ if(isset($r)){ $var['Referrer']=$r; unset($r); } elseif(isset($HTTP_REFERER)){ $var['Referrer'] = parse_url($HTTP_REFERER); $var['ReferrerHost'] = $var['Referrer']['host']; $var['ReferrerString'] = $var['Referrer']['path']; if(isset($var['Referrer']['query'])){ $var['ReferrerString'] .= "?" . $var['Referrer']['query']; } } else{ $var['ReferrerHost'] = "Direct"; $var['ReferrerString'] = ""; } srand(time()); $var['RandSessionLength'] = 8; $var['RandSessionPool'] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $var['RandSessionPool'] .= "abcdefghijklmnopqrstuvwxyz"; $var['RandSessionPool'] .= "123456789"; $var['SessionID'] = ""; for($var['i']=0;$var['i']<$var['RandSessionLength']; $var['i']++){ $var['SessionID'] .= substr($var['RandSessionPool'],(rand()%(strlen($var['RandSessionPool']))), 1); } $var['SessionID'] .= '.'.time(); $var['SQL'] = "INSERT INTO $var[SessionsTable] VALUES('$var[SessionID]','$HTTP_HOST','$var[ReferrerHost]','$REMOTE_ADDR', '$HTTP_USER_AGENT','$var[Datetime]','$var[Datetime]',1,'$REQUEST_URI?$var[Ti mestamp]','$var[ReferrerString]')"; setcookie("varSessionID",$var['SessionID']); } else{ $var['SQL'] = "UPDATE $var[SessionsTable] SET pagecount=pagecount+1,pages=CONCAT(pages,'|$REQUEST_URI?$var[Timestamp]'),en dtime='$var[Datetime]'"; $var['SQL'] .= " WHERE session='$var[SessionID]'"; } if($debug==TRUE) echo $var['SQL']."\n"; mysql_db_query($var['dbDatabase'],$var['SQL']); unset($var); ?> Have fun kids.... "Peter Lovatt" <pjln1@sunmaia.net> wrote in message news:NFBBKAGAILLHPFOOAKFIIELGCLAA.pjln1@sunmaia.net... > Hi > > I use > > $sessionID = md5(uniqid(rand())); > > setcookie (sessionID , $sessionID ,0 , "" , "" ,0); > > which works > > hth > > Peter > > > -----Original Message----- > > From: jas [mailto:jlgerfen@hotmail.com] > > Sent: 22 January 2002 06:51 > > To: php-db@lists.php.net > > Subject: [PHP-DB] tracking users... > > > > > > I am fairly new to PHP and have setup a site that connects to a > > MySQL db to > > store different information from our users, however what I would like to > > setup is something that I could place on a few pages to track users for > > demographic purposes. What I need to do is as a page is visited I need to > > generate a random alpha numeric identifier to be used as a > > session variable > > for a cookie and also have that cookie's information be placed > > into a MySQL > > database for later retrieval. If anyone has a good resource tutorial on > > this type of function I would greatly appriciate it. Oh and yes > > I have done > > quite a bit of research on php.net on both the session() and the > > setcookie() > > functions. =) Thanks again. Jas > > > > > > > > > > -- > > PHP Database Mailing List (http://www.php.net/) > > To unsubscribe, e-mail: php-db-unsubscribe@lists.php.net > > For additional commands, e-mail: php-db-help@lists.php.net > > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.db (#16065) next »