luckatpf Posted July 6, 2009 Share Posted July 6, 2009 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 More sharing options...
shergold Posted July 6, 2009 Share Posted July 6, 2009 ok... ignore that im a noob -.- shergold. Link to comment https://forums.phpfreaks.com/topic/164928-solved-session-data-lost/#findComment-869694 Share on other sites More sharing options...
PFMaBiSmAd Posted July 6, 2009 Share Posted July 6, 2009 session_id() only returns the current session id after a session_start(). session_id() would only be used before a session_start() if you are trying to set the session id. Link to comment https://forums.phpfreaks.com/topic/164928-solved-session-data-lost/#findComment-869701 Share on other sites More sharing options...
luckatpf Posted July 6, 2009 Author Share Posted July 6, 2009 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? Link to comment https://forums.phpfreaks.com/topic/164928-solved-session-data-lost/#findComment-869706 Share on other sites More sharing options...
shergold Posted July 6, 2009 Share Posted July 6, 2009 ok heres your answer, on the second page place start_session() before you try to echo the session id. it will then echo the same session id as the last page. edit: ignore my older post about giving a value to it, i got confused. Shergold. Link to comment https://forums.phpfreaks.com/topic/164928-solved-session-data-lost/#findComment-869709 Share on other sites More sharing options...
shergold Posted July 6, 2009 Share Posted July 6, 2009 no, the session_id() automatically creates the id for you. each page you want to use the session on i believe you need to use session_start(). Link to comment https://forums.phpfreaks.com/topic/164928-solved-session-data-lost/#findComment-869711 Share on other sites More sharing options...
luckatpf Posted July 6, 2009 Author Share Posted July 6, 2009 Thanks, I solved it by moving the session_start() line. Link to comment https://forums.phpfreaks.com/topic/164928-solved-session-data-lost/#findComment-869713 Share on other sites More sharing options...
shergold Posted July 6, 2009 Share Posted July 6, 2009 no problem. shergold. Link to comment https://forums.phpfreaks.com/topic/164928-solved-session-data-lost/#findComment-869714 Share on other sites More sharing options...
pacchiee Posted July 6, 2009 Share Posted July 6, 2009 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. Link to comment https://forums.phpfreaks.com/topic/164928-solved-session-data-lost/#findComment-869716 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.