- 精品下载 | 实用查询 | 词典查询 | 桌面壁纸 | 网址 | 笑话 | FLASH频道 | 天气文章资讯 | 站长工具 | 证件办理 | 闪字生成 | 广告代码 | 在线手册 | 有问必答
您现在的位置: 蓝派网 >> 文章中心 >> 网络编程 >> PHP >> 正文
站内文章搜索:           

用Session对Web页面进行保护

作者:佚名    文章来源:网络转载    更新时间 :2007-11-26 23:31:09
要求目的:同一站点,无权用户,一般授权用户和超级用户能看到和使用不同的页面。

实现办法:在要保护的页面 include 不同级别的安全检验摸板。

注意事项
1 $#@62; 要避免用户浏览器不使用
cookie 而不能浏览受保护的页面(session 默认使用客户端的 cookie).
2 $#@62; 要防止权限被盗用。(
PHP 4 的 session 的默认存活期间是从建立 session 开始到关闭浏览器为止。)

如何使用
1 $#@62; 在需要一般保护的页面的代码最前边加上 include ("secturity2.php"); 就行了
2 $#@62; 在需要特殊保护的页面的代码最前边加上 include ("secturity1.php"); 和 include ("secturity2.php"); 就行了
(假设所有文件都在同一个文件夹里)

程序代码及详细解释
security1.php 特殊用户页面保护摸板
security2.php 一般用户页面保护摸板
login2.php 用户登陆页面

我们先来看 login2.php (用户登陆页面)的代码:

$#@60;?php
session_register("user");#增加用户名变数
session_register("password"); #增加密码变数
session_register("tmLast"); #增加时间变数
if($user==""){#判断是否是第一次登陆
$error="Chooseyounameandinputthepasswordplease!";
}
$tmLast=date("
U"); #记录登陆时间
if($user1)
$user=
trim($user1); #记录用户名(引用user1变量是为什么?请读者自己思考。)
$password=
trim($password1); #记录密码
if($user1&&$password1){
if($password1==888){ #判断登陆密码是否是默认密码888结束 PHP 程式
$sid="PHPSESSID=".session_id(); #保存当前session的ID号
$warning="Yourpasswordisstillthedefaultpassword888,pleasechangeit.";
header("Location:changePassword.php?$sid&warning=$warning"); #传递警告参数warning到changePassword.php 页面
exit(); #立刻结束 PHP 程式
}
if(strtolower($user)=="
root"){ #判断登陆用户是否是超级用户,可以自行扩充用户
$fil
me="backend_index.php";
}
else{
if(!$fileName) #判断进入登陆页面的
上一页是否是受保护页面
$fileName="index.php";
}
$sid="PHPSESSID=".session_id(); #保存当前session的ID号
header("Location:$fileName?$sid); #登陆成功进入指定页面,传递当前session的ID号,防止用户不使用 cookie 而读不到 session 值
exit(); #立刻结束 PHP 程式
}
?$#@62;
$#@60;html$#@62;
$#@60;title$#@62;$#@60;/title$#@62;
$#@60;head$#@62;
$#@60;linkrel="stylesheet"href="class/
style.css"$#@62;
$#@60;metahttp-equiv="Content-Type"content="text/html;charset=gb2312"$#@62;
$#@60;/head$#@62;
$#@60;
h2$#@62;LoginPage$#@60;/h2$#@62;
$#@60;?php
echo"$error"; #显示登陆提示
?$#@62;
$#@60;formaction="$#@60;?phpecho$PHP_SELF;#提交到当前页?$#@62;"method=post$#@62;
$#@60;P$#@62;$#@60;b$#@62;Name:$#@60;/b$#@62;
$#@60;?php
include("class/dbclass.inc"); #调用dbclass.inc类,用法和
mysql.inc 类一样
$q=newDB_
Sql; #定义一个新的对象
$q-$#@62;connect($Host,$Database,$User,$Password); #连接
mysql数据库
$query="selectchrUserName,chrFirstName,chrLastName".
"fromUser".
"wherechrFirstName!=".
"orderbychrFirstName";
$q-$#@62;query($query); #执行sql
语句
echo"$#@60;selectname=user1size=1$#@62;";
while($q-$#@62;next_record()){ #从数据库中调出一般用户
if($user==$q-$#@62;f(0)) #判断是否是当前用户
$select="selected"; #是当前用户则设置为默认值
else
$select="";
echo"$#@60;optionvalue=".$q-$#@62;f(0)."$select$#@62;".
ucfirst($q-$#@62;f(1))."". #用户名首字大写
ucfirst($q-$#@62;f(2))."$#@60;/option$#@62;";
}
echo"$#@60;/select$#@62;";
?$#@62;$#@60;/P$#@62;
$#@60;P$#@62;$#@60;b$#@62;Password:$#@60;/b$#@62;$#@60;INPUTname=password1type=password$#@62;$#@60;/P$#@62;
$#@60;INPUTname=tmLasttype=hiddenvalue=$#@60;?phpechodate("U")?$#@62;$#@62;
$#@60;INPUTname=fileNametype=hiddenvalue=$#@60;?phpecho$fileName?$#@62;$#@62;
$#@60;P$#@62;$#@60;INPUTname=submittype=submitvalue=确认$#@62;$#@60;/P$#@62;
$#@60;/
form$#@62;

security2.php (一般用户页面保护摸板):
$#@60;?php
session_register("user"); #说明同上
session_register("password");
session_register("tmLast");
if($fileName=="")
$fileName=$PHP_SELF; #记录当前页面路径
if($durtime=="")
$durtime=300; #设置 session “失效”时间
$currtime=date("U");
if(($currtime-$tmLast)$#@62;$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-$#@62;connect($Host,$Database,$User,$Password);

$query="selectidUserfromUser".
"wherechrUserName=$user".
"andchrPasswd=$password";
$q-$#@62;query($query);

if(!$q-$#@62;num_
rows

[1] [2] 下一页


发表评论】【打印此文】【关闭窗口】【点击数:
★好玩的休闲小游戏★
网友评论:
数据载入中,请稍后……