Wolverine68 Posted March 7, 2009 Share Posted March 7, 2009 I had this problem last week and thought I had it corrected, but when I go back and test it again it's not working. The user logs in by entering a name and password. The name and password are hard coded into the login page. When submitted it goes to a display page that determines if the correct name and password were entered. If they are correct, it's suppose to display "Welcome, ........". If not, it's suppose to display "You have entered an invalid...." If I enter the correct name and password, it does display the "Welcome...." message. But, if I enter an incorrect name and/or password, it still displays the "Welcome...." message. Why isn't it displaying the "You have entered an invalid....." message? Login. php page: <?php $_SESSION['name'] = "ken"; $_SESSION['password'] = "welcome"; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Session Variables</title> </head> <body> <div> <form action="display.php" method="POST"> <p>Enter your username and password to login:</p> <p>Name:<INPUT TYPE="text" SIZE="20" name="name"></p> <p>Password:<INPUT TYPE="text" SIZE="20" name="password"></p> <p><INPUT TYPE="submit" VALUE="Submit!"></p> </form> </div> </body> </html> Display.php page: <?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Validating Session</title> </head> <body> <div> <?php if (($_POST['name'] == $_SESSION['name']) && ($_POST['password'] == $_SESSION['password'])) { $_SESSION['Active'] = "True"; } else { $_SESSION['Active'] = "False"; } ?> <?php If ($_SESSION['Active'] = "True") { echo "Welcome, " .$_POST['name']. "you have successfully logged into your session."; } else { echo "You have entered an invalid username and/or password. Access denied"; } ?> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/148373-solved-problem-validating-a-session/ Share on other sites More sharing options...
wildteen88 Posted March 7, 2009 Share Posted March 7, 2009 This If ($_SESSION['Active'] = "True") { should be If ($_SESSION['Active'] == "True") { Quote Link to comment https://forums.phpfreaks.com/topic/148373-solved-problem-validating-a-session/#findComment-778972 Share on other sites More sharing options...
Wolverine68 Posted March 7, 2009 Author Share Posted March 7, 2009 I made the change as you suggested but now if the correct name and password are entered, I get "You have entered an invalid....." message. Now that message comes up no matter what is entered. Login. php page: <?php $_SESSION['name'] = "ken"; $_SESSION['password'] = "welcome"; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Session Variables</title> </head> <body> <div> <form action="display.php" method="POST"> <p>Enter your username and password to login:</p> <p>Name:<INPUT TYPE="text" SIZE="20" name="name"></p> <p>Password:<INPUT TYPE="text" SIZE="20" name="password"></p> <p><INPUT TYPE="submit" VALUE="Submit!"></p> </form> </div> </body> </html> Display.php page: <html> <head> <title>Validating Session</title> </head> <body> <div> <?php if (($_POST['name'] == $_SESSION['name']) && ($_POST['password'] == $_SESSION['password'])) { $_SESSION['Active'] = "True"; } else { $_SESSION['Active'] = "False"; } ?> <?php If ($_SESSION['Active'] == "True") { echo "Welcome, " .$_POST['name']. "you have successfully logged into your session."; } else { echo "You have entered an invalid username and/or password. Access denied"; } ?> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/148373-solved-problem-validating-a-session/#findComment-778990 Share on other sites More sharing options...
Wolverine68 Posted March 7, 2009 Author Share Posted March 7, 2009 I didn't get all of the display.php code copied in there. Here it is again: <?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Validating Session</title> </head> <body> <div> <?php if (($_POST['name'] == $_SESSION['name']) && ($_POST['password'] == $_SESSION['password'])) { $_SESSION['Active'] = "True"; } else { $_SESSION['Active'] = "False"; } ?> <?php If ($_SESSION['Active'] == "True") { echo "Welcome, " .$_POST['name']. "you have successfully logged into your session."; } else { echo "You have entered an invalid username and/or password. Access denied"; } ?> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/148373-solved-problem-validating-a-session/#findComment-778992 Share on other sites More sharing options...
wildteen88 Posted March 7, 2009 Share Posted March 7, 2009 Any page that uses sessions much have session_start() as the first line. You do not have session_start() in your login page. Quote Link to comment https://forums.phpfreaks.com/topic/148373-solved-problem-validating-a-session/#findComment-778994 Share on other sites More sharing options...
Wolverine68 Posted March 7, 2009 Author Share Posted March 7, 2009 moved session_start() to the login page (and removed it from the display page) but I still have the same problem. Login. php: <?php session_start(); $_SESSION['name'] = "ken"; $_SESSION['password'] = "welcome"; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Session Variables</title> </head> <body> <div> <form action="display.php" method="POST"> <p>Enter your username and password to login:</p> <p>Name:<INPUT TYPE="text" SIZE="20" name="name"></p> <p>Password:<INPUT TYPE="text" SIZE="20" name="password"></p> <p><INPUT TYPE="submit" VALUE="Submit!"></p> </form> </div> </body> </html> Display.php: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Validating Session</title> </head> <body> <div> <?php if (($_POST['name'] == $_SESSION['name']) && ($_POST['password'] == $_SESSION['password'])) { $_SESSION['Active'] = "True"; } else { $_SESSION['Active'] = "False"; } ?> <?php If ($_SESSION['Active'] == "True") { echo "Welcome, " .$_POST['name']. "you have successfully logged into your session."; } else { echo "You have entered an invalid username and/or password. Access denied"; } ?> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/148373-solved-problem-validating-a-session/#findComment-779002 Share on other sites More sharing options...
wildteen88 Posted March 7, 2009 Share Posted March 7, 2009 No you still need it within your display page session_start() must be called in any page that uses $_SESSION's Quote Link to comment https://forums.phpfreaks.com/topic/148373-solved-problem-validating-a-session/#findComment-779004 Share on other sites More sharing options...
Wolverine68 Posted March 7, 2009 Author Share Posted March 7, 2009 Ok, got it working now. But, I'm still not quite understanding why I would have to also put session_start() on the display page. When the form is submitted and the display page called, isn't the session_start() variable "carried" over? The name and password that are entered on the login page are, since the code on the display page is comparing the values entered by the user with the values hard coded into the login page. Quote Link to comment https://forums.phpfreaks.com/topic/148373-solved-problem-validating-a-session/#findComment-779017 Share on other sites More sharing options...
wildteen88 Posted March 7, 2009 Share Posted March 7, 2009 Like I said. In order for sessions to work you have to call session_start() in any page that uses $_SESSIONS's if you don't call session_start() PHP wont be able to carry over the $_SESSION variables. $_POST has not relation to sessions. Quote Link to comment https://forums.phpfreaks.com/topic/148373-solved-problem-validating-a-session/#findComment-779028 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.