<html> <head> <title>网站后台管理系统</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <link href="inc/admin.css" mce_href="inc/admin.css" rel="stylesheet" type="text/css"> </head> <body> <TABLE cellSpacing=0 cellPadding=0 width="95%" border=0> <TBODY> <TR> <TD vAlign=top height=138><BR> <FORM ACTION="checklogin_cookie.php" METHOD="POST"> <TABLE height=159 cellSpacing=0 cellPadding=0 width=268 align=center bgColor=#ffff99 border=0> <TBODY> <TR> <TD align=middle width=234 height=43><IMG height=113 src="images/login.gif" width=359></TD></TR> <TR> <TD align=middle background=images/loginbg.gif bgColor=#fafafa height=180> <TABLE width=250 border=0> <TBODY> <TR> <TD align=right height=25 width="70">帐号:</TD><Td><INPUT tabIndex=1 maxLength=20 size=15 name=username></TD></TR> <TR> <TD align=right width="70">密码:</TD><Td> <INPUT tabIndex=2 type=password maxLength=20 size=15 name=password></TD></TR> <tr> <td align=right width="70">保存时间:</TD><Td> <select name="cookie" id="cookie"> <option value="0" selected>浏览器进程</option> <option value="1">保存1天</option> <option value="2">保存30天</option> <option value="3">保存365天</option> </select></td> </tr> <TR> <TD align=middle height=25 colspan="2"><INPUT id=login_manager style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BACKGROUND-IMAGE: url(images/loginbutton.gif); BORDER-LEFT: 0px; WIDTH: 52px; CURSOR: hand; BORDER-BOTTOM: 0px; HEIGHT: 18px" type=submit value=" " name=login_manager> <INPUT id=login_member2 style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BACKGROUND-IMAGE: url(images/loginbutton2.gif); BORDER-LEFT: 0px; WIDTH: 52px; CURSOR: hand; BORDER-BOTTOM: 0px; HEIGHT: 18px" type=reset value=" " name=login_member2></TD></TR> <TR> <TD align=middle> <TABLE border=0> <TBODY> <TR> <TD></TD> <TD width=10></TD> <TD></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR> <TR> <TD align=middle background=images/loginend.gif height=5></TD></TR></TBODY></TABLE></FORM></TD></TR></TBODY></TABLE> </body> </html> <?php // 链接数据库 require_once('../Connections/lr.php'); mysql_select_db($database_lr, $lr); //获取用户输入 $username = $_POST['username']; $password = md5($_POST['password']); $cookie = $_POST['cookie']; //执行SQL语句 $query = @mysql_query("select username,password,userflag from admin " ."where username = '$username' and password = '$password'") or die("SQL语句执行失败"); //判断用户是否存在,密码是否正确 if($row = mysql_fetch_array($query)) { if($row['userflag'] == 1 or $row['userflag'] == 0) //判断用户权限信息是否有效 { switch($cookie) //根据用户的选择设置cookie保存时间 { case 0: //保存Cookie为浏览器进程 setcookie("username", $row['username']); break; case 1: //保存1天 setcookie("username", $row['username'], time()+24*60*60); break; case 2: //保存30天 setcookie("username", $row['username'], time()+30*24*60*60); break; case 3: //保存365天 setcookie("username", $row['username'], time()+365*24*60*60); break; } echo("<meta http-equiv=refresh content='0; url=index.php'>"); } else { echo "用户权限信息不正确"; } } else { echo "用户名或密码错误"; } ?> 在需要验证的页面顶部 <?php session_start(); if(isset($_COOKIE['username'])) { require_once('../Connections/lr.php'); //获取Session $username = $_COOKIE['username']; //执行SQL语句获得userflag的值 $query = @mysql_query("select userflag from admin " ."where username = '$username'") or die("SQL语句执行失败"); $row = mysql_fetch_array($query); //获得用户权限信息 $flag = $row['userflag']; //根据userflag的值输出不同的欢迎信息 if($flag == 1) echo ""; if($flag == 0) echo ""; echo ""; } else { echo "<mce:script language='javascript'><!-- alert('请重新登陆');window,location='login.php'; // --></mce:script>"; } ?>