Jump to content

Session variable


vchris
 Share

Recommended Posts

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...