Page 1 of 1

php Login page error

Posted: Sat Feb 04, 2012 11:24 pm
by phpjawahar
Someone help me on this....

I want to check the username and password i enter in to the form.
The problem i face is, though i submit the form with wrong password 'success_page.php' get processed.

Here is my codes.....

This is login.php

Code: Select all

<?php
error_reporting(0);
$username = "jawahar";
$password = "welcome";

if(isset($username) && isset($password)) {
 if(($username = $_REQUEST['username']) && ($password = $_REQUEST['password']))
 {
  header('location:success_page.php');
 }
 else
 {
  $msg = "Login failed!";
 }
}
else
{
 $username = "";
 $password = "";
}
?>

<html>
<head>
<title>Login Page</title>
</head>
<body>
<div align="center">
<form name="loginfrm" action="" method="post">
User Name: <input type="text" name="username" /><br /><br>
Password : <input type="password" name="password" /><br /><br>
<input type="submit" name="submit" value="Login" />
<div align="center"><?php echo $msg; ?></div>
</form>
</div>


</body>
</html>

And this is success_page.php

Code: Select all

<html>
<head>
<title>Login success</title>
</head>

<body>
<div align="center" style="color:#FF0000; font-size:20px;">
 You have Successfully Logged in!...
</div>
</body>
</html>

Re: php Login page error

Posted: Sun Feb 05, 2012 12:50 am
by flying_circus
phpjawahar wrote:Someone help me on this....

Code: Select all

if(($username = $_REQUEST['username']) && ($password = $_REQUEST['password']))
The problem starts right here. Notice the use of a single equal sign. What you are checking, is if $_REQUEST['username'] is successfully set to the variable of $username. I think what you intend to do, is check if $_REQUEST['username'] is equal to $username. In that case, you need to use 2 equal signs "==" to make it a comparative operater, rather than an assignment operator.


More Information:
Assignment Operators
Comparison Operators


You should also not use the $_REQUEST superglobal. Instead, if you expect the data to come from a form POST, then use $_POST. If you expect the data to come from the url querystring, then use $_GET. If you expect the data to come from a cookie, use $_COOKIE.

More Information:
$_REQUEST superglobal

Re: php Login page error

Posted: Sun Feb 05, 2012 2:37 am
by phpjawahar
Thanks Mr. Flying_circus,

On changing the single '=' in to '==', it is working fine.

Need help on two more things.
1. My form display the message of "Login failed!", before submitting the values.
2. How to use SESSION concept here. That is., i want to display the name of the user who has entered in to the page.

With Regards,
Jawahar

Re: php Login page error

Posted: Sun Feb 05, 2012 7:45 am
by Celauran
phpjawahar wrote:1. My form display the message of "Login failed!", before submitting the values.

Code: Select all

<?php

$username = "jawahar";
$password = "welcome";

// This check is pointless since you have explicitly set these values above
if(isset($username) && isset($password))
{
    // Here you're checking if the form has been submitted and the username/password match
    if(($username == $_REQUEST['username']) && ($password == $_REQUEST['password']))
    {
        header('location:success_page.php');
    }
    // Fail if they don't match
    // This will also fail if the form hasn't been submitted
    else
    {
        $msg = "Login failed!";
    }
}
// This will never be executed.
else
{
    $username = "";
    $password = "";
}

?>
instead, try something like this:

Code: Select all

<?php

$username = "jawahar";
$password = "welcome";

// Check if the form has been submitted
if (!empty($_POST))
{
    // Now check that the form values match the values above
    if(($username == $_POST['username']) && ($password == $_POST['password']))
    {
        header('location:success_page.php');
    }
    // Fail if they don't match
    else
    {
        $msg = "Login failed!";
    }
}

?>
phpjawahar wrote:2. How to use SESSION concept here. That is., i want to display the name of the user who has entered in to the page.
Sessions

Re: php Login page error

Posted: Sun Feb 05, 2012 9:01 am
by phpjawahar
Hi celauran,

Thanks for your coding and making me clear.

Re: php Login page error

Posted: Mon Feb 06, 2012 9:31 am
by Mordred
put an exit(); after header();