博客项目(二)
显示登录的信息
登录成功后,在管理员页面显示登录的信息。
用户的登录信息保存在会话中,从会话中取出对应的值就可以了。
运行结果
退出和安全退出
退出:退出不销毁会话
安全退出:退出并销毁会话
- 在LoginController控制器中,添加安全退出的方法
- 点击top.html页面中安全退出,跳转到logout()方法上
不同级别的用户显示不同的导航
测试:
- 用管理员身份登录
- 用普通身份登录
记住密码
登录成功后,将用户信息保存到客户端,后面登录的时候就不需要再输入用户信息。
代码实现
- 在配置文件中添加秘钥
- 在UserModel模型中,将用户信息记录在cookie中
脚下留心:不能直接将用户名和密码记录在cookie中,不安全(可以直接在浏览器中查看)。
- 在UserModel模型中,通过cookie的值获取用户的信息
- 登录成功后,如果选择了记录密码就将信息保存到cookie中
- 请求登录页面并且不是从其他页面跳转过来,就试图通过cookie获取用户
验证码
思路
第一步:生成随机字符串
第二步:将字符串写到图片资源上
第三步:显示图片
代码实现
- 在Lib文件夹下创建Captcha.class.php页面
<?php
namespace Lib;
class Captcha{
private $len; //验证码位数
private $font; //内置字体 1,2,3,4,5
public function __construct($len=4,$font=5) {
$this->len=$len;
$this->font=$font;
}
//生成随机字符串
private function createRandomString(){
$char_array= array_merge(range(‘a’, ‘z’), range(‘A’, ‘Z’), range(0, 9));
$index=array_rand($char_array, $this->len); //随机取出字符串,返回下标数组
shuffle($index); //打乱数组
$str=”;
foreach($index as $i){
$str.=$char_array[$i];
}
$_SESSION[‘verify’]=$str;
return $str;
}
//创建验证码
public function createVerify(){
$str= $this->createRandomString();
$image= imagecreate(80, 30);
imagecolorallocate($image, 255, 255, 255);
$color= imagecolorallocate($image, 255, 0, 0);
$x=(imagesx($image)-imagefontwidth($this->font)*$this->len)/2;
$y=(imagesy($image)-imagefontheight($this->font))/2;
imagestring($image, $this->font, $x, $y, $str, $color);
header(‘Content-Type:image/png’);
imagepng($image);
imagedestroy($image);
}
//检查验证码是否正确
public function checkVerify($code){
return strtoupper($code)==strtoupper($_SESSION[‘verify’]);
}
}
- 调用生成验证码类
- 在login.html页面中显示验证码
- 验证输入的验证码
session入库
将会话保存到数据库中
创建会话表
drop table if exists `session`;
create table `session`(
sess_id varchar(50) primary key comment ‘会话id’,
sess_value varchar(2000) comment ‘会话的值’,
sess_time int not null comment ‘会话产生的时间’
)engine=memory charset=utf8 comment ‘会话表’;
表结构
代码实现
1、在Lib文件下创建Session.class.php页面
<?php
/*
* session入库
*/
namespace Lib;
class Session{
private $mypdo;
public function __construct() {
//设置会话存储方式
session_set_save_handler(
array($this,’open’),
array($this,’close’),
array($this,’read’),
array($this,’write’),
array($this,’destroy’),
array($this,’gc’)
);
session_start(); //开启会话
}
public function open(){
$this->mypdo= \Core\MyPDO::getInstance($GLOBALS[‘config’][‘database’]);
}
public function close(){
return true;
}
public function read($sess_id){
$sql=”select sess_value from `session` where sess_id=’$sess_id'”;
return $this->mypdo->fetchColumn($sql);
}
public function write($sess_id,$sess_value){
$time= time();
$sql=”insert into `session` values (‘$sess_id’,’$sess_value’,$time) on duplicate key update sess_value=’$sess_value'”;
return $this->mypdo->exec($sql);
}
public function destroy($sess_id){
$sql=”delete from `session` where sess_id=’$sess_id'”;
return $this->mypdo->exec($sql);
}
public function gc($maxlifetime){
$expires=time()-$maxlifetime; //过期时间的临界点
$sql=”delete from `session` where sess_time<$expires”;
return $this->mypdo->exec($sql);
}
}
2、在Controller控制器中实例化session入库类
查看数据库中的会话
- 能够完成后台登录的验证码功能
- 能够完成添加文章分类的增删改功能
- 能够以递归方式树状显示文章分类列表
- 能够完成文章的增删改和显示功能
- 能够完成文章列表的翻页功能
- 能够实现博客前台首页显示功能
- 能够实现博客前台文章列表功能
- 能够实现博客前台显示文章详情功能
- 能够实现博客文章的踩赞功能
- 能够实现博客文章的上一篇下一篇功能
- 能够实现博客文章的评论功能
- 能够实现博客文章的搜索功能
- 能够实现博客文章的标签功能
- 能够理解博客项目的主要功能
- 能够根据功能要求画出有关数据表的ER图
- 能够针对项目要求设计适当的数据表