Forum Moderators: coopster

Message Too Old, No Replies

PHP session created on each page load

Continued from thread: http://www.webmasterworld.com/php/3013540.htm

         

Bingo

2:34 pm on Nov 7, 2006 (gmt 0)

10+ Year Member



Hi, I'm having a battle with session variables in PHP5 and need some help.


<?php

session_start();

if (!isset($_SESSION['id'])) {
$_SESSION['id']=session_id();;
}

else {
echo $_SESSION['id'];
}

?>

I'm running XAMPP PHP5 on WinXP SP2, testing with Firefox.

When I disable cookies, the code runs fine, but the session id is not being retained in the session variable 'id'. Also, a new session is being created in my /tmp every time I reload the page.

I want the script to store the sessionid in a session variable across pages, not a cookie!

Thanks,
Nick

coopster

2:50 pm on Nov 7, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have you read through the Session Handling Functions [php.net] to see how to manage sessions in this fashion? I would guess you might not have a directive set correctly or you are not passing the ID correctly.

mcibor

10:47 pm on Nov 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try reading the manual

[php.net...]

Note: When using session cookies, specifying an id for session_id() will always send a new cookie when session_start() is called, regardless if the current session id is identical to the one being set.

Try this out:

<?php

if(!isset($_SESSION)) session_start();

$_SESSION['id'] = strip_tags(SID);

echo $_SESSION['id'];
?>

Hope this helps
Regards
Michal

Bingo

10:33 am on Nov 13, 2006 (gmt 0)

10+ Year Member



Hi,

Thank for the help. Michal I tried your suggestion but it still produces a new SID on every page refresh. I have tried this with and without cookies enabled, in Firefox and IE, same result.

Nick

mcibor

1:39 pm on Nov 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



But does it save session?

Try this:

test.php:

<?php
if(!isset($_SESSION)) session_start();

if(isset($_SESSION['test']))
{
echo $_SESSION['test'];
}
else
{
$_SESSION['test'] = "Session is set and remembered, no matter the SID";
echo 'Session was not set. <a href="test.php">Click here</a> to refresh';
}
?>

After you click on the link, session should be set and remember the data.
Michal