Wolverine68 Posted March 4, 2009 Share Posted March 4, 2009 I've created a login page. The user enters a name and a password. If the name and password entered match the name and password hard coded into the form, the session is active, otherwise, it is not. When the form is submitted, it goes to a page that displays a message indicating whether or not the login was successful. Even if the name and password entered are correct, it still displays the message, "You have entered an invalid username and/or password. Access denied" Login page: <?php session_start(); $_SESSION['name'] = "ken"; $_SESSION['password'] = "welcome"; if (($_POST['name'] == $_SESSION['name']) && ($_POST['password'] == $_SESSION['password'])) { $_SESSION['Active'] == "True"; } else { $_SESSION['Active'] == "False"; } ?> <!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 page: <!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 ($_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> Link to comment https://forums.phpfreaks.com/topic/147830-solved-problem-with-session-variables-in-a-form/ Share on other sites More sharing options...
samshel Posted March 4, 2009 Share Posted March 4, 2009 your form is submitting to display.php, and the code to validate username/password is in login.php... you will get $_POST values in display.php, so shift ur code to display.php Link to comment https://forums.phpfreaks.com/topic/147830-solved-problem-with-session-variables-in-a-form/#findComment-775922 Share on other sites More sharing options...
ricmetal Posted March 4, 2009 Share Posted March 4, 2009 on the first ifs $_SESSION['Active'] == "True"; should be $_SESSION['Active'] = "True"; i believe Link to comment https://forums.phpfreaks.com/topic/147830-solved-problem-with-session-variables-in-a-form/#findComment-775923 Share on other sites More sharing options...
samshel Posted March 4, 2009 Share Posted March 4, 2009 and ricmetal is right too Link to comment https://forums.phpfreaks.com/topic/147830-solved-problem-with-session-variables-in-a-form/#findComment-775925 Share on other sites More sharing options...
fry2010 Posted March 4, 2009 Share Posted March 4, 2009 would he also need to do session_start(); on the second document too? Link to comment https://forums.phpfreaks.com/topic/147830-solved-problem-with-session-variables-in-a-form/#findComment-775937 Share on other sites More sharing options...
Wolverine68 Posted March 4, 2009 Author Share Posted March 4, 2009 I moved the code over to display.php page as you suggested. However, the only message that displays is "Welcome, " .$_POST['name']. "you have successfully logged into your session." That message displays even if the wrong name and/or password are entered. Why isn't it recognizing the "You have entered an invalid username and or password..." statement? Login.php code: <?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 code <!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> Link to comment https://forums.phpfreaks.com/topic/147830-solved-problem-with-session-variables-in-a-form/#findComment-775951 Share on other sites More sharing options...
ricmetal Posted March 4, 2009 Share Posted March 4, 2009 = means youre equaling a to b a = b == means a is equal to b a == b so if(a == b) { c= "yeah!"; } Link to comment https://forums.phpfreaks.com/topic/147830-solved-problem-with-session-variables-in-a-form/#findComment-775954 Share on other sites More sharing options...
samshel Posted March 4, 2009 Share Posted March 4, 2009 If ($_SESSION['Active'] == "True") { in if condition u have to use "==" and put session_start() at the start of second page too.. Link to comment https://forums.phpfreaks.com/topic/147830-solved-problem-with-session-variables-in-a-form/#findComment-775955 Share on other sites More sharing options...
Wolverine68 Posted March 4, 2009 Author Share Posted March 4, 2009 It's working now. Thanks! Link to comment https://forums.phpfreaks.com/topic/147830-solved-problem-with-session-variables-in-a-form/#findComment-775962 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.