Jump to content

[SOLVED] Problem with session variables in a form.


Wolverine68

Recommended Posts

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>

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.