Jump to content

[SOLVED] Session Data Lost


luckatpf

Recommended Posts

Hi, i'm building a login script for my site using Sessions and Cookies to keep the user logged in. The problem itself is, session_id() returns an empty value after i start the session and the user tries to follow some link.

 

I actually read the php sessions documentation but i think i'm missing something.

Tried this and it reproduces the behaviour of my login code:

 

Page1.php

<?php
session_start();
echo 'Session ID is '.session_id().'<br>';
echo '<a href="Page2.php">Go to Page 2</a>';
?>

 

Page2.php

<?php
if (session_id()=''){
echo 'Session ID is empy??';
}else{
echo session_id();
session_start();
}
?>

 

When i Reach page1 i get the

So when i click Page2 link, i always get an empty session id. Is this normal behaviuor?

I thought session_id kept its value, im not using sessions in URLs options are set to cookies, also checked the gc_max... and are all set so it doesnt timeout

Im using XAMPP to develop, and since i thought it may be a xampp issue, i switched to my Apache + PHP server and the behaviour is the same... Tested on Opera and Internet explorer

Any help, comment is welcome. Thanks.

Link to comment
https://forums.phpfreaks.com/topic/164928-solved-session-data-lost/
Share on other sites

If by assigning a value you mean something like $_SESSION['somevar']='some value' that doesnt change the behaviour, else is it i have to assign a session_id manually???

 

-----------------------------------------------------------------------

Ok now i get the idea, so its pointless to check for an existing session id unless i set it manually.

Then how do i check for an existing session? just by checking for some session variable?

Page1.php

<?php
session_start();
echo 'Session ID is '.session_id().'<br>';
echo '<a href="Page2.php">Go to Page 2</a>';
?>

 

Page2.php

<?php
session_start();
if (session_id()=''){
echo 'Session ID is empy??';
}else{
echo session_id();
}
?>

 

That should solve the problem.

 

Always session_start() should be at the top of anything else in a php file when using Sessions.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.