AdamBrave Posted February 18, 2013 Share Posted February 18, 2013 Hi, I'm building a site where I have to login so I want to use a session to keep my user ID. How can I use the user ID stored in the session variable when I go to another page of my site different from the login page? Quote Link to comment https://forums.phpfreaks.com/topic/274632-sessions-in-php/ Share on other sites More sharing options...
Jessica Posted February 18, 2013 Share Posted February 18, 2013 Did you read the manual? http://www.php.net/manual/en/session.examples.php http://www.php.net/manual/en/session.examples.basic.php Quote Link to comment https://forums.phpfreaks.com/topic/274632-sessions-in-php/#findComment-1413142 Share on other sites More sharing options...
andrew_biggart Posted February 18, 2013 Share Posted February 18, 2013 To set: $_SESSION["uid"] = "Your value"; To retrieve: echo $_SESSION["uid"]; Quote Link to comment https://forums.phpfreaks.com/topic/274632-sessions-in-php/#findComment-1413146 Share on other sites More sharing options...
AyKay47 Posted February 18, 2013 Share Posted February 18, 2013 There is also a nice tutorial on this topic provided by phpfreaks: http://www.phpfreaks.com/tutorial/sessions-and-cookies-adding-state-to-a-stateless-protocol Quote Link to comment https://forums.phpfreaks.com/topic/274632-sessions-in-php/#findComment-1413148 Share on other sites More sharing options...
AdamBrave Posted February 18, 2013 Author Share Posted February 18, 2013 (edited) I know how to store the id on a session variable and I can see it if I remain in the welcome page (where I defined the session) but when I jump to another page and try to print my ID I get the warning: "Notice: Undefined variable: _SESSION on line 234 IDuser= " Edited February 18, 2013 by AdamBrave Quote Link to comment https://forums.phpfreaks.com/topic/274632-sessions-in-php/#findComment-1413151 Share on other sites More sharing options...
Jessica Posted February 18, 2013 Share Posted February 18, 2013 (edited) Code? Sounds like your session isn't starting. Edited February 18, 2013 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/274632-sessions-in-php/#findComment-1413154 Share on other sites More sharing options...
AdamBrave Posted February 18, 2013 Author Share Posted February 18, 2013 <?php session_start(); ?> <html> <body> <?php . . . result = mysql_query("SELECT * FROM userinformation WHERE mail LIKE '$maill' AND password LIKE '$passwordd'"); $row = mysql_fetch_array($result); $_SESSION['userID'] = $row['id']; . . . ?> </body> Quote Link to comment https://forums.phpfreaks.com/topic/274632-sessions-in-php/#findComment-1413164 Share on other sites More sharing options...
Jessica Posted February 18, 2013 Share Posted February 18, 2013 That code produces that error? And session_start() is the very first line? Quote Link to comment https://forums.phpfreaks.com/topic/274632-sessions-in-php/#findComment-1413165 Share on other sites More sharing options...
AdamBrave Posted February 18, 2013 Author Share Posted February 18, 2013 The error appears when I do the following "echo" in another file different from the "welcome file" where I defined the session start: <?php //retrieve session data echo "IDuser=". $_SESSION['userID']; echo "<br />"; echo "<br />"; ?> And yes, the "session_start()" is the very first line. Quote Link to comment https://forums.phpfreaks.com/topic/274632-sessions-in-php/#findComment-1413174 Share on other sites More sharing options...
Jessica Posted February 18, 2013 Share Posted February 18, 2013 And in the other file you have session_start() as the first line too? Quote Link to comment https://forums.phpfreaks.com/topic/274632-sessions-in-php/#findComment-1413178 Share on other sites More sharing options...
AdamBrave Posted February 18, 2013 Author Share Posted February 18, 2013 Ops!! Problem solved!! I thought that was only necessary in one file...my bad LOL Thanks!!! Quote Link to comment https://forums.phpfreaks.com/topic/274632-sessions-in-php/#findComment-1413180 Share on other sites More sharing options...
AyKay47 Posted February 18, 2013 Share Posted February 18, 2013 To clarify, session_start() is needed in any script where you are using sessions. Quote Link to comment https://forums.phpfreaks.com/topic/274632-sessions-in-php/#findComment-1413182 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.