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.
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.' ;
}
?>
You can set a SESSION flag and when calling ClientLogin.
$_SESSION['loginerror'] = 1;
in ClientLogin.php
if ($_SESSION['loginerror'] > 0) {
/* Display Appropriate Error message */
}
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.
来源:https://stackoverflow.com/questions/18814483/incorrect-username-password-message-and-redirect