php Login page error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpjawahar
Forum Newbie
Posts: 19
Joined: Thu Jan 12, 2012 6:06 am
Location: Chennai, India

php Login page error

Post 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>
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: php Login page error

Post 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
phpjawahar
Forum Newbie
Posts: 19
Joined: Thu Jan 12, 2012 6:06 am
Location: Chennai, India

Re: php Login page error

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: php Login page error

Post 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
phpjawahar
Forum Newbie
Posts: 19
Joined: Thu Jan 12, 2012 6:06 am
Location: Chennai, India

Re: php Login page error

Post by phpjawahar »

Hi celauran,

Thanks for your coding and making me clear.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: php Login page error

Post by Mordred »

put an exit(); after header();
Post Reply