0

Okay...so...my first question on here, please bear with me.

I have made a login script with registration, MySQL database, etc. So far it is working great. Except for the fact that when I refresh the page, even if I have not entered any information yet, it gives me the incorrect login information error. First time Loading the page, or if i log into a fake user, then log out, it will work. But Here is the code

    if($_SESSION["logged"])
{
         print_secure_content();
}
else {
        if(!$_SESSION["logging"])
        {  
        $_SESSION["logging"]=true;
        echo "Login:";
        loginform();
        }
         else if($_SESSION["logging"])
           {
                 $number_of_rows=checkpass();
                 if($number_of_rows==1)
                        {       
                         $_SESSION[user]=$_POST[userlogin];
                         $_SESSION[logged]=true;
                         echo"<h1>you have logged in successfully</h1>";
                         print_secure_content();
                        }
                else{
                                   echo "wrong password or username, please try again";
                                    loginform();



                        }
                }

         }

1 Answer 1

1

That's because you're storing whether or not to validate the username/password in the $_SESSION.

When the login form is shown, $_SESSION['logging'] is set to true. Then, you log in successfully (and it's still set to true). The next time you see the login form, it's still set to true, so it attempts to check your username/password combination and thus the error.

Rather than using $_SESSION['logging'] to determine whether or not you should validate the username/password, you should check for the existence of your username and password fields in the $_POST array, since these will be present only when the user submits the form.

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

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.