<?php
//定义一个用户组
$users=array(
array("username"=>"a","password"=>"1","style"=>"css1"),
array("username"=>"b","password"=>"2","style"=>"css2"),
array("username"=>"c","password"=>"3","style"=>"css3"),
array("username"=>"d","password"=>"4","style"=>"css4"),
array("username"=>"e","password"=>"5","style"=>"css5"),
array("username"=>"f","password"=>"6","style"=>"css6"),
);
//定义函数,检查用户是否登录
function is_login(){
//global关键字,使用user数组
global $users;
//cookie值赋给新变量
$u=$_COOKIE['username'];//读取数组中键名为username元素的值
$p=$_COOKIE['passward'];//读取数组中名为password元素的值
//遍历用户组
foreach ($users as $key=>$value){
//比较cookie中的值与用户组中的值
if($value["username"]==$u and $value["password"]==$p){
//如果相等,返回true
return TRUE;
}
}
return FALSE;
//遍历数组后,没有相等的值,返回false
}
//定义一个函数,用户登录后的cookie
function login(){
global $users;
//把$_POST数组中的单元赋给新变量
$u=$_POST["username"];
$p=$_POST["password"];
//遍历用户数组
foreach ($users as $key=>$value){
//查找表单中提交的变量,检查是否与用户组中的一组值相等
if($value["username"]==$u and $value["password"]==$p){
//如果表单提交的变量等于数组中的值,设置cookie值,供is_login()函数检查
setcookie("username",$value["username"]);
setcookie("password",$value["password"]);
setcookie("style",$value["style"]);
//使用JavaScript显示登录信息,转向用户页
echo "<script>alert('登录成功!');</script>";//显示登录成功信息
echo "<script>window.navigate('renzheng.php');</script>";
return true;
//跳转到其他页面
}
}
//遍历完数组,没有相等的信息,显示登录错误信息,跳转其他页
echo "<script>alert('用户名或密码错误!');</script>";
echo "<script>window.navigate('renzheng.php');</script>";
return false;
}
//定义函数用于删除cookie,完成注销
function logout(){
//消除cookie
setcookie("username","");//将username中的元素值设置为空
setcookie("password","");
//显示注销信息
echo "<script>alert('注销成功');</script>";
echo "<script>window.navigate('renzheng.php');</script>";
}
//定义用户登录表单
function loginTable(){
print<<<EOD
<table width="300" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="?action=login">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>用户名:</td>
<td><label>
<input name="username" type="text" id="username">
</label></td>
</tr>
<tr>
<td>密码:</td>
<td><label>
<input name="password" type="text" id="password">
</label>
</td>
</tr>
<tr>
<td colspan="2"><label>
<input type="submit" name="Submit" value="提交">
</label></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
EOD;
}
switch($_GET['action'])
{
case "login":
login();
break;
case "logout":
logout();
break;
}
?>
<!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>用户登录</title>
</head>
<body>
<?php
if (is_login()){
?>
<div class="css"> 你好:<?=$_COOKIE['username'];?> <a href='?action=logout'>注销</a></div>
<div class='<?=$_COOKIE['style']?>'>用户登录后,显示内容.</div>
<?php
}else{
loginTable();
}
?>
</body>
</html>
叶0528
lambert_见