Incorrect username / password message and redirect

2019-04-15 21:48发布

问题:

Creating a Client Login area. I have my log in form in a clientLogin.html:

       <form name="ClientLogin" method="post" action="login.php"> <br>
            <p>
             <label for='Username'>Username</label> <br>
            <input type="text" name="username" />
            </p>
            <p>

            <label for='Password'>Password</label> <br>
            <input type="password" name="password" />
            </p>
            <br>
            <input type="submit" value="Submit" />

        </form>

If the username & password combination are incorrect I want to redirect to this page adding a message saying "incorrect username/ password please try again". This is the code from login.php:

<?php


//Connect to the database
    require('db.php');

    $user = mysql_real_escape_string($_POST["username"]);
    $pass = mysql_real_escape_string($_POST["password"]);

    $clientdata = mysql_query("SELECT * FROM Users WHERE username='$user' and            password='$pass'")
    or die (mysql_error());

    $data = mysql_fetch_array($clientdata, MYSQL_ASSOC);

    if(mysql_num_rows($clientdata) == 1){

session_start();


$_SESSION['username'] = $user;


header('Location: profile.php');

    }else{
header('Location: clientLogin.html');
    }

I expect the clientLogin.html files needs changing to a .php file. other than that I am stumped. Any guidance would be greatly appreciated.

回答1:

You need to change extension of clientLogin.html from .html to .php

Change header('Location: clientLogin.html'); to header('Location: clientLogin.php');

Add this part of code at the top of your clientLogin.php page:

<?php 
session_start();
if(empty($_SESSION['username'])) {
 echo 'incorrect username/ password please try again.' ;
}
?> 

   



回答2:

You can set a SESSION flag and when calling ClientLogin.

    $_SESSION['loginerror'] = 1;

in ClientLogin.php

    if ($_SESSION['loginerror'] > 0) {
      /* Display Appropriate Error message */
    }


回答3:

you should use exit(); after redirecting to other file like..

header('Location: profile.php');
exit();

you please use mysqli instead of mysql .its not going to be supported by new version of php.