Jump to content

Session variable


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 post
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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...