vchris Posted August 25, 2006 Share Posted August 25, 2006 This must be a pretty n00b question but I can't seem to get the session variable to work. This is a login script. On successful login, I create a session variable called userid which contains in this case the number 1. Then on the second page (page redirected to) I check to make sure the user id logged in with this condition if(isset($_SESSION['userid'])){ . Doesn't work, I'm redirected to the first page (when userid is not set in session). I then tried this echo $_SESSION['userid']; exit(); and got this error Undefined variable: _SESSION . Is there somewhere I need to set this variable? I did a previous script like this and everything seemed to work fine... Link to comment Share on other sites More sharing options...
SFB Posted August 25, 2006 Share Posted August 25, 2006 without seing the whole script its hard to tell but check if you have session_start() at the top of each page you use the session on. Link to comment Share on other sites More sharing options...
vchris Posted August 26, 2006 Author Share Posted August 26, 2006 I knew it was this simple. SFB you just solve the issue :)I checked my old script and I see session_start(); at the top. When setting a session variable when logging in I don't need to start the session right? Only on a new page where I use a session variable?Thanks. Link to comment Share on other sites More sharing options...
SFB Posted August 26, 2006 Share Posted August 26, 2006 I knew it was this simple. SFB you just solve the issue :)I checked my old script and I see session_start(); at the top. When setting a session variable when logging in I don't need to start the session right? Only on a new page where I use a session variable?Thanks.um I'm not sure if that is true, I always use session_start(); on all the pages i use sessions on. even the ones i set them on. I guess it doesnt hurt to use it if you dont have to but i think you should. Link to comment Share on other sites More sharing options...
murfitUK Posted August 27, 2006 Share Posted August 27, 2006 You NEED session_start() on all the pages that reference the session. It checks to see if a session exists. If not , it creates a new session. If a session already exists it uses the data that's already there.Sometimes it seem silly having to do it immediately before you want to destroy a session, so your code looks like:session_start();session_destroy();but it has to be done! Link to comment Share on other sites More sharing options...
vchris Posted August 28, 2006 Author Share Posted August 28, 2006 Thanks for the tip guys Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now