1

I am making this simple code for authentication of a user. the page with the form is : >login.php. with the following code.

login.php :

<html>    
<body>  

<div style="position:absolute;left:300px;top:300px;width:300px;height:100px;z-index:9;>  

<form name="form1" method="POST" action="check.php">  
Username:<br />  
  <input type="text" name="Username" />  
  <br /><br />   
  Password:<br />  
  <input type="password" name="Password" />  
  <br /><br /><br/>  
  <input type=button onClick="location.href='check.php'" value='Continue'       name='continue'/>  
  </form>  
</div>  
</body>  
</html>  

the form field values are being used in the next php page with following code:

check.php :

<?php
session_start();
$con = mysql_connect("localhost", "root", "");
mysql_select_db("aviation", $con) or die(mysql_error());
if (isset($_POST['continue'])) {
    $userName = $_POST[Username];
    $passWord = $_POST[Password];
    mysql_select_db('aviation');
    $query = "select * from users where Username='" . $userName . "'and     Password='" . $passWord . "'";
    $result = mysql_query($query, $con);
    if (!$result) {
        die('Could not enter data: ' . mysql_error());
    }
    $rows = mysql_num_rows($result);
    if ($rows == 1) {
        $_SESSION['Username'];
        $_SESSION['Password'];
        echo "Successful";
        echo "<BR>";
        echo "You are authorized to update the status of Bays.";
        echo "<BR>";
        $Msg = "Redirecting....";
        echo '<script type="text/javascript">  
         alert("' . $Msg . '");  
  </script>';
        header("location:upbstatus.php");
    } elseif ($userName == "" || $passWord == "") {
        $errorMsg = "Data was not entered <br/> Enter Username and Password";
        echo '<script type="text/javascript">  
         alert("' . $errorMsg . '");  
  </script>';
        else {
            $errorMsg = "Data Does Not Match <br/> Re-Enter Username and Password";
            $errorMsg = "Data was not entered <br/> Enter Username and Password";
            echo '<script type="text/javascript">  
         alert("' . $errorMsg . '");  
  </script>';
        }
    } else {
        echo ("  =============== not SET ===============");
    }
?>

It always return : =============== not SET ===============

I am so stuck at why is this happening.Can anybody help please? It'l be appreciated.
Thankyou.

Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
Sara Khurshid
  • 11
  • 1
  • 2

4 Answers4

2

Your onclick (onClick="location.href='check.php'") event handler is requesting check.php before the form can make a POST request. Once the button is clicked it redirects to check.php which sends a GET request to check.php NOT POST.

To fix it you need to change the button to a submit typed input.

   <input type='submit' value='Continue' name='continue' />  

Additional information:

  1. Quote array indices. Like $_POST['Username']
  2. Escape parameters in sql using mysql_real_escape_string. or Use prepared statements.
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
1

There are a few issues with the code:

Weird form behaviour

<input type=button onClick="location.href='check.php'" value='Continue'       name='continue'/>  

Why are you doing this? This wouldn't send the username and password fields at all, because the browser will redirect before the form is submitted. If you want to submit the form when clicked, use <input type="submit">

<input type="submit" value="Continue" name="continue" />

Quote array indices

$userName = $_POST[Username];  
$passWord = $_POST[Password];  

This causes notices to be raised in PHP because the constant Username doesn't exist. Use this instead:

$userName = $_POST['Username'];
$passWord = $_POST['Password'];

I won't get into the scenario where $_POST['Username'] might not be set.

Escape variables in SQL

$query = "select * from users where Username='".$userName."'and     Password='".$passWord."'";  

This is dangerous; you should escape the variables properly:

$query = sprintf("select * from users where Username='%s' and Password='%s'",
    mysql_real_escape_string($userName),
    mysql_real_escape_string($passWord)
);

Don't store passwords in plain text

Storing passwords in plain text is just asking for trouble. Use bcrypt to store a hash of the password instead.

See also: Secure hash and salt for PHP passwords

Don't use mysql_xx functions

This feature is deprecated in favour of PDO / mysqli and prepared statements.

Community
  • 1
  • 1
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

Remove on click in your submit button. While your at your it, fix your quotes.

frustratedtech
  • 423
  • 4
  • 9
0
<form name="form1" method="POST" action="check.php">  
Username:<br />  
  <input type="text" name="Username" />  
  <br /><br />   
  Password:<br />  
  <input type="password" name="Password" />  
  <br /><br /><br/>  
  <input type="submit" value="Continue" name="continue"/>  
  </form> 
Xfile
  • 674
  • 8
  • 19