简明现代魔法 -> PHP服务器脚本 -> PHP 简单的密码登陆验证
PHP 简单的密码登陆验证
2010-03-23
登录页面 login.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<table width="600" border="0" align="center" style="font-family:Verdana,宋体;font-size: 12px;">
<form action="check.php" method="post" onSubmit="return chkinput(this)">
<tr>
<td width="160" height="25" align="right"> </td>
<td width="400"><span class="STYLE1"></span></td>
</tr>
<tr>
<td height="25" align="right">用户名 User Name:</td>
<td> <input type="text" name="name" size="40" maxlength="80" value="" class="inputcss" />
<span class="STYLE1"> </span></td>
</tr>
<tr>
<td height="25" align="right">密 码 Password: </td>
<td> <span class="STYLE1">
<input type="text" name="password" size="40" maxlength="80" value="" class="inputcss" />
</span></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="提交" class="buttoncss" /> <input type="reset" value="重置" class="buttoncss"></td>
</tr>
</form>
</table>
</body>
</html>
验证 check.php
<?php
// session开始
session_start();
$name = $_POST['name'];
$password = $_POST['password'];
$_SESSION['name'] = "NowaMagic";
$_SESSION['password'] = "HelloWorld";
if ($password != $_SESSION['password'] && $name != $_SESSION['name']){
echo "密码错误!";
echo "<script>alert('密码错误'); history.go(-1);</script>";
}
else if ($password == $_SESSION['password'] && $name == $_SESSION['name']){
$page="MessageBoardManager.php";
echo "<script>window.location = \"".$page."\";</script>";
}
?>
然后在你想加密的页面上加这么一段代码:
<?php
// session开始
session_start();
if ("HelloWorld" != $_SESSION['password'] && "NowaMagic" != $_SESSION['name']){
$page="login.php";
echo "<script>window.location = \"".$page."\";</script>";
}
?>

