2

I seem to be having a strange problem all the sudden and I was hoping someone may have a suggestion...

I have a login script that is supposed to register a session variable containing an error message if the login fails and then redirect the user to the page they came from. For example the user may have used the forum on index.php and the login fails and they are returned to index.php where a script displays the error message contained in the session variable.

However, the session variables do not appear to be saving. For the record, I am using session_start() in the login script as well as any page that has a login form that should display the error message if the user is returned to that page because the login failed.

My script is as follows:

if (isset($_POST['prev'])) {
  $prev = $_POST['prev'];
}
else {
  $prev = "login.php";
}
$_SESSION['Login_Error'] = $error;
header("Location: $prev");

Then the script on the form pages is:

if (isset($_SESSION['Login_Error'])) {
  echo $_SESSION['Login_Error'];
}

And the error I am getting is:

Notice: Undefined index: Login_Error in F:\EasyPHP-12.0\www\index.php on line 3

Any ideas as to why it isn't saving? If the login is successful the script sets a user id session variable which is working fine. Thanks for any suggestions.

5
  • to use session variable you need to start the session before it ...and make sure that you have the same domain name Commented Nov 29, 2012 at 4:34
  • Just to be sure, are you calling session_start()? There should be no reason for the error as far as I can see, especially since you use isset to check for existence. If you are indeed calling session_start() and are not getting any errors regarding the saving of sessions (check your error logs, make sure files are being created in the session store directory...) then we'll need to see more code. Commented Nov 29, 2012 at 4:36
  • I am using session_start() on the first line of index.php and login.php Commented Nov 29, 2012 at 4:36
  • can you show complete code?, also how to store userid, etc. in SESSION Commented Nov 29, 2012 at 4:37
  • with that code its looks fine ... Commented Nov 29, 2012 at 4:38

3 Answers 3

4

I have had this problem before. If the session variable is set while the user is on

www.domain.com/login.php

and the user is redirected to

domain.com/index.php (without the www. at the beginning)

The session variable will not be accessible. Make sure that the www. is either always there, never there, or that your script uses it or not depending on the circumstance.

Sign up to request clarification or add additional context in comments.

Comments

1

At some point further down the line, if you have: unset($_SESSION['Login_Error']) or similar.. . like $_SESSION['Login_Error'] = null for example.. this can cause an issue.

The reason this is - is that after the header to redirect is sent - the rest of the php script continues to execute.

If you place a die() or exit() after the header.. this should then stop the rest of the script executing and thus, make sure the var isn't unset at any point further down the script.

Hope this helps.

Comments

0

You require to start session before you declare session variables:

http://php.net/manual/es/function.session-start.php

also there is a bug on slowly machines where can't save session variables just at the moment.

try using a timeout header:

header("refresh:3;url=".$prev);

See more about refresh header at http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Refresh

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.