undefined index error in login form

Mahim Parsai

I have started to learn php and mysql and I have created a login form. I want to show error when username or password is incorrect. But when the page is opened it shows Notice: Undefined index: user and Undefined index: pass, and i have included the php code below the password input box because I want to show the error below it, but I keep getting this error.And if I enter any wrong username and click on login then this error doesn't appear. Please help

<html>
<head>
</head>
<body>
<form action="login.php" method="post">
<label for="username">Username</label>
<input name="user" type="text" id="username" required="" >
<label for="password">Password</label>
<input name="pass" type="password" id="password" required="" >
<?php
// start session
session_start();
include'connect.php';
// Define $username and $password 
$username=$_POST['user']; 
$password=$_POST['pass']; 
$sql="SELECT * FROM table WHERE username='$username' and password='$password'";
$result=mysqli_query($con,$sql);
// Mysql_num_row is counting table row $count=mysqli_num_rows($result);
// If result matched $username and $password, table row must be 1 row
if($count==1){
$_SESSION['username'] = $_POST['user'];
// goto to welcome page
header('Location: welcome.php');
}
else {
// show error
echo "<div style='color:red;' class='errorbox'>Incorrect Username or Password</div><br>";
}
?>
<button type="submit">Log In</button>
</form>
</body>
</html>
ameenulla0007

you need to format your script like below.

<?php
//start session
session_start();
include'connect.php';
// Define $username and $password
if(isset($_POST["user"])) {
    $username   = mysqli_real_escape_string($_POST['user']); 
    $password   = mysqli_real_escape_string($_POST['pass']); 
    $sql        = "SELECT * FROM table WHERE username='$username' and password='$password'";
    $result = mysqli_query($con,$sql);
    $count      = mysqli_num_rows($result);
    if($count==1) {
        $_SESSION['username'] = $_POST['user'];
        // goto to welcome page
        header('Location: welcome.php');
    }
    else {
        // show error
        echo "<div style='color:red;' class='errorbox'>Incorrect Username or Password</div><br>";
    }
}
?>
<html>
<head>
</head>
<body>
<form action="login.php" method="post">
<label for="username">Username</label>
<input name="user" type="text" id="username" required="" >
<label for="password">Password</label>
<input name="pass" type="password" id="password" required="" >
<button type="submit" name="login-now">Log In</button>
</form>
</body>
</html>

points to be noted

  1. session_start(); needs to be started before everything, there should not be any html code before it.

  2. use of condition with isset which indicates the submit button is clicked and value of post is set, if it is set, it means the other requests of username and password is also ready to request.

(if you directly execute statements without isset, the requests won't be ready to request, and if requested, it returns with an error, undefined index)

  1. As a reminder by @Alex, user of mysqli_real_escape_string, this protects your script with SQL injections.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related