php 不正确的用户名/密码消息和重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18814483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Incorrect username / password message and redirect
提问by SJBenton
Creating a Client Login area. I have my log in form in a clientLogin.html:
创建客户端登录区域。我在 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:
如果用户名和密码组合不正确,我想重定向到此页面并添加一条消息“用户名/密码不正确,请重试”。这是来自 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.
我希望 clientLogin.html 文件需要更改为 .php 文件。除此之外,我很难过。任何指导将不胜感激。
回答by Cyril Joudieh
You can set a SESSION flag and when calling ClientLogin.
您可以在调用 ClientLogin 时设置 SESSION 标志。
$_SESSION['loginerror'] = 1;
in ClientLogin.php
在 ClientLogin.php 中
if ($_SESSION['loginerror'] > 0) {
/* Display Appropriate Error message */
}
回答by Charaf JRA
You need to change extension of clientLogin.html from .html
to .php
您需要将 clientLogin.html 的扩展名从 更改.html
为.php
Change header('Location: clientLogin.html');
to header('Location: clientLogin.php');
更改header('Location: clientLogin.html');
为header('Location: clientLogin.php');
Add this part of code at the top of your clientLogin.php
page:
在clientLogin.php
页面顶部添加这部分代码:
<?php
session_start();
if(empty($_SESSION['username'])) {
echo 'incorrect username/ password please try again.' ;
}
?>
? ?
? ?
回答by Rajeev Ranjan
you should use exit(); after redirecting to other file like..
你应该使用 exit(); 重定向到其他文件后,如..
header('Location: profile.php');
exit();
you please use mysqli
instead of mysql
.its not going to be supported by
new version of php.
你请使用mysqli
而不是mysql
.its 不会被新版本的 php 支持。