Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Occasional session problem 2

Status
Not open for further replies.

mancroft

Programmer
Oct 26, 2002
267
GB
Occasional session problem

Hello

I have done a logger: thelogger.php.

This usually works OK BUT sometimes a user lands on the site, sets the session ID as 4re3ccc...etc and then goes to another file and a new session ID xc3zkf...etc gets set.

The user agent appears to be a standard browser e.g.

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)

and not a crawler.

Any idea as to the cause and solution?

Thank you.

Here is the code at the top of the logger file. This file is accessed at the top of every php file by using:

Code:
<?php include("thelogger.php"); ?>

The GetTheId() function is used to get the session ID when putting it into the database.

Code:
<?php session_start();
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");	

function GetTheId(){ 
if(isset($_COOKIE["theId"])){
return $_COOKIE["theId"];
} 
else
{
session_start();
setcookie("theId", session_id(), time() + 36000, "/", "",0);
return session_id();
}
}
 
I'm a bit confused about your code. Have you configured your PHP installation's session system to use a session cookie named "theId"?



Want the best answers? Ask the best questions! TANSTAAFL!
 
no. it is just the name i gave to the session cookie.
 
If you haven't told PHP to use your session cookie, how does it know to use it?

In php.ini there is the runtime configuration directive session.name. When a script invokes session_start(), PHP looks for a cookie the name of which matches the value of session.name. PHP then uses that cookie's value to open the session store and unserialize the session variables.



Want the best answers? Ask the best questions! TANSTAAFL!
 
oh. didn't know that. duh! will get that set up. thank you.
 
Better still, why tinker with the session ID or session cookie at all?

Your code will normally never need to know the session ID.

PHP will set the session ID cookie as needed automatically.



Want the best answers? Ask the best questions! TANSTAAFL!
 
OK, so when it come to inserting the sessionid into the database as below, instead of using the GetTheId(), how do I do that?

Code:
INSERT INTO thestats (thetime,referer,sessionid)VALUES(now(),'$referer','" . GetTheId() . "')
 
Duh!!! Thanks, jpadie.

Bleedin' obvious really, innit?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top