使用 PHP
你可以简单地做这样的事情
$failed=true;
if $email && $password !=$zz || $email && $password !=$sql{
$failed=false;
}
以及稍后找到此特定段落的位置:
echo "<p ". ($failed?"hidden":"")+">Wrong password!</p>
不同的页面
我建议使用这样的不同页面:
login
|--index.php //loginform
|--login.php
|--fail
| |--index.php //with link to login form
home
|--index.php
includes
|--sql.php
登录/index.php
<html>
<head>
<title>
Login
</title>
<body>
<form action="login.php" method="POST">
<table>
<tr><td>User</td><td><input name="user" placeholder="johndoe123/></td></tr>
<tr><td>Pass</td><td><input type="password" name="pass" placeholder="iluvyou%&-"/></td></tr>
<tr colspan=2><td><input type="submit" value="Login" /></td></tr>
</table>
</form>
</body>
</html>
login/login.php
注意:使用准备好的语句!!!您使用的方法极易受到 SQL 注入和 XSS 的攻击。另外:请对密码进行哈希处理!
<?php
include "/includes/sql.php";
$user=$_POST["user"];
$passtry=$_POST["pass"];
$sql="SELECT * FROM users WHERE user='?'";
$cmd = $con->prepare($sql);
$cmd->execute(array($user));
if($entry=$cmd->fetchObject()){
$pass=$entry->pass;
}else{
header("Location: fail");
exit(0);
}
if(password_verify($passtry,$pass)){
session_start();
$_SESSION["login"]=true;
$_SESSION["user"]=$user;
header("Location: ../home");
}else{
header("Location: fail");
exit(0);
}
?>
失败/index.html
<html>
<head>
<title>
Wrong password!
</title>
</head>
<body>
<p>Wrong password. <a href="..">Try again!</a></p>
</body>
</html>
主页/index.html
<?php
session_start();
if(!$_SESSION["login"]){
header("Location: ../login/fail");
exit();
}
?>
<html>
<head>
<title>
<?php echo $_SESSION["user"] ?> - Home
</title>
</head>
<body>
PRIVATE CONTENT!!!
</body>
</html>
includes/sql.php 取决于你的 sql 引擎。
注意:在您的注册表单中,您必须使用password_hash()保存密码