您好,欢迎访问一九零五行业门户网

用Session对Web页面进行保护

session|web|页面
在很多时候,我们都要对某些web 页面进行安全保护。典型的例子就是前台浏览页面与后台管理页面的安全性。这也是 web 上用得最多的一种页面安全模式。在用php4 开发一个小型的书籍管理系统中,我也遇到了这个安全问题。于是我想到了php4 的新特性----session ..
要求目的:同一站点,无权用户,一般授权用户和超级用户能看到和使用不同的页面。
实现办法:在要保护的页面 include 不同级别的安全检验摸板。
注意事项:
1 > 要避免用户浏览器不使用 cookie 而不能浏览受保护的页面(session 默认使用客户端的 cookie).
2 > 要防止权限被盗用。( php 4 的 session 的默认存活期间是从建立 session 开始到关闭浏览器为止。)
如何使用:
1 > 在需要一般保护的页面的代码最前边加上 include (secturity2.php); 就行了
2 > 在需要特殊保护的页面的代码最前边加上 include (secturity1.php); 和 include (secturity2.php); 就行了
(假设所有文件都在同一个文件夹里)
程序代码及详细解释:
security1.php 特殊用户页面保护摸板
security2.php 一般用户页面保护摸板
login2.php 用户登陆页面
我们先来看 login2.php (用户登陆页面)的代码:
loginpage
method=post>
name:
connect($host,$database,$user,$password); #连接 mysql数据库
$query=selectchrusername,chrfirstname,chrlastname.
fromuser.
wherechrfirstname!=''.
orderbychrfirstname;
$q->query($query); #执行sql语句
echo;
while($q->next_record()){ #从数据库中调出一般用户
if($user==$q->f(0)) #判断是否是当前用户
$select=selected; #是当前用户则设置为默认值
else
$select=;
echof(0).'$select>.
ucfirst($q->f(1)).. #用户名首字大写
ucfirst($q->f(2)).;
}
echo;
?>
password:
>
>
security2.php (一般用户页面保护摸板):
$durtime){ #判断 session是否“失效”
//session_destroy();
$error=urlencode(seesionexpired.loginagainplease!);
header(location:login2.php?filename=$filename&error=$error&user=$user); #跳到重新登陆页
exit();
}
else{
$tmlast=$currtime; # session 没“失效”则更新最后“登陆”时间
}
include(class/dbclass.inc);
$q=newdb_sql;
$q->connect($host,$database,$user,$password);
$query=selectiduserfromuser.
wherechrusername='$user'.
andchrpasswd='$password';
$q->query($query);
if(!$q->num_rows()){ #判断是否找到密码匹配的用户
$error=urlencode(passwordiswrongornoprivilegeuser.);
header(location:login2.php?filename=$filename&error=$error&user=$user); #跳到密码错误登陆页
}
else{
$sid=phpsessid=.session_id();
$q->next_record();
$userid=$q->f(iduser); #保存通过验证用户的id号,方便以后使用
}
?>
security1.php (特殊用户页面保护摸板):
其它类似信息

推荐信息