先在数据库当中创建以下数据:
mysql> create table user(-> id int not null auto_increment,-> username varchar(50) not null default '',-> password char(32) not null default '',-> email varchar(80) not null default '',-> allow_1 smallint not null default 0,-> allow_2 smallint not null default 0,-> allow_3 smallint not null default 0,-> primary key(id)); mysql> insert into user(username,password,email,allow_1,allow_2,allow_3) values('admin',md5('admin'),'admin@admin.com',1,1,1); Query OK, 1 row affected (0.18 sec)mysql> insert into user(username,password,email,allow_1,allow_2,allow_3) values('shuaige',md5('shuaige'),'shuai@admin.com',0,1,1); Query OK, 1 row affected (0.12 sec)mysql> insert into user(username,password,email,allow_1,allow_2,allow_3) values('daniu',md5('daniu'),'daniu@admin.com',0,0,1); Query OK, 1 row affected (0.14 sec)
allow_x中的一代表用户的权限,其值中的 1代表拥有权限,0代表没有权限。看一下结构。
mysql> select * from user; +----+----------+----------------------------------+-----------------+---------+---------+---------+ | id | username | password | email | allow_1 | allow_2 | allow_3 | +----+----------+----------------------------------+-----------------+---------+---------+---------+ | 1 | admin | 21232f297a57a5a743894a0e4a801fc3 | admin@admin.com | 1 | 1 | 1 | | 2 | shuaige | 37d2b9990df5a6843caf19352fee42a6 | shuai@admin.com | 0 | 1 | 1 | | 3 | daniu | 76720c5adee75ce9c7779500893fb648 | daniu@admin.com | 0 | 0 | 1 | +----+----------+----------------------------------+-----------------+---------+---------+---------+ 3 rows in set (0.00 sec)
然后我们写几个文件夹:
- Index.php 首页
- Cookie.php Cookie页面
- Content.php 文章页面
- Exit.php 离开页面
第一个Index.php
<?php echo "你好这是首页.<br />"; echo "<a href='login.php'>管理员页面登陆</a>";?>
第二个Cookie.php
<?php header("content-type:text/html;charset=utf-8"); //判断是否存在并且判断是否已经登陆的反句。 if ((isset($_COOKIE['isLogin']) && $_COOKIE['isLogin'] == 1)) {echo "抱歉,您还没有登陆呢.";//不能直接使用header来跳转。echo '<script>setTimeout(\'location="login.php"\',30000);</script>'; }?>
第三个Login.php
<?php header("content-type:text/html;charset=utf-8");include './cookie.php';if (isset($_POST['dosubmit'])) {$pdo = new pdo("mysql:host=localhost;dbname=admin","root","");$sql = $pdo->prepare("select id,username,password,allow_1,allow_2,allow_3 from user where username =? and password=? ");$sql->execute(array($_POST['username'],md5($_POST['password'])));if($sql -> rowCount() > 0){//rowCount是执行sql影响的行数。//list:把数组中的一些值赋值给变量,$sql->fetch(PDO::FETCH_NUM)是用来获取值的,然后组成 一个数组。list($id,$username,$password,$allow_1,$allow_2,$allow_3) = $sql->fetch(PDO::FETCH_NUM);echo "用户ID:".$id."<br />";echo "用户账号:".$username."<br />";echo "用户密码:".$password."<br>";echo "用户权限:".$allow_1."<br>";echo "用户权限:".$allow_2."<br>";echo "用户权限:".$allow_3."<br>";$time = time()+24*60*60;//当前时间加上一天的时间//setcookie(name,value,time,path)setcookie("uid",$id,$time,"/");//将$id存储在uid里面。setcookie("username",$username,$time,"/");setcookie("passworod",$password,$time,"/");setcookie("allow_1",$allow_1,$time,"/");setcookie("allow_2",$allow_2,$time,"/");setcookie("allow_3",$allow_3,$time,"/");header("location:content.php");}else{echo "<script>alert('密码错误!')</script>";}}?> <html> <h3>用户登陆</h3> <form action="" name='form1' method='POST'> username:<input type="text" name="username" value='' /><br> password:<input type="password" name="password" value='' /><br /><input type="submit" name='dosubmit' value='登陆'><br /> </form> </html>
第四个Exit.php
<?php if (isset($_COOKIE["username"])) {//问以下是否设置了usernamesetcookie("id","",time()-3600,"/");setcookie("username","",time()-3600,"/");setcookie("password","",time()-3600,"/");setcookie("allow_1","",time()-3600,"/");setcookie("allow_2","",time()-3600,"/");setcookie("allow_3","",time()-3600,"/");echo "再见,亲爱的{$_COOKIE['username']}"; }?><script>setTimeout("location='index.php'",3000);</script>