选择显示字体大小

用session对web页面进行保护

     用session对web页面进行保护
  · 曾宪宏·cpcw
  
    在很多时候,我们都要对某些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 (用户登陆页面)的代码:
  
  <?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"){ #判断登陆用户是否是超级用户,可以自行扩充用户
  $filename="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 程式
  }
  ?>
  <html>
  <title></title>
  <head>
  <linkrel="stylesheet"href="class/style.css">
  <metahttp-equiv="content-type"content="text/html;charset=gb2312">
  </head>
  <h2>loginpage</h2>
  <?php
  echo"$error"; #显示登陆提示
  ?>
  <formaction="<?phpecho$php_self;#提交到当前页?>"method=post>
  <p><b>name:</b>
  <?php
  include("class/dbclass.inc"); #调用dbclass.inc类,用法和 mysql.inc 类一样
  $q=newdb_sql; #定义一个新的对象
  $q->connect($host,$database,$user,$password); #连接 mysql数据库
  $query="selectchrusername,chrfirstname,chrlastname".
  "fromuser".
  "wherechrfirstname!=''".
  "orderbychrfirstname";
  $q->query($query); #执行sql语句
  echo"<selectname=user1size=1>";
  while($q->next_record()){ #从数据库中调出一般用户
  if($user==$q->f(0)) #判断是否是当前用户
  $select="selected"; #是当前用户则设置为默认值
  else
  $select="";
  echo"<optionvalue='".$q->f(0)."'$select>".
  ucfirst($q->f(1))."". #用户名首字大写
  ucfirst($q->f(2))."</option>";
  }
  echo"</select>";
  ?></p>
  <p><b>password:</b><inputname=password1type=password></p>
  <inputname=tmlasttype=hiddenvalue=<?phpechodate("u")?>>
  <inputname=filenametype=hiddenvalue=<?phpecho$filename?>>
  <p><inputname=submittype=submitvalue=确认></p>
  </form>
  
  security2.php (一般用户页面保护摸板):
  <?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)>$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 (特殊用户页面保护摸板):
  <?php
  session_register("user"); #说明同上
  $privilege="root,macro,jackie"; #设置超级用户名单列表,用“,”隔开
  $pieces=explode(",",$privilege); #取得单个超级用户名单
  for($i=0;$i<count($pieces);$i++){
  if(strtolower($user)==$pieces[$i]){ #判断是否是超级用户
  $hasprivilege=1;
  break; #跳出判断循环
  }
  }
  
  if(!$hasprivilege){
  if($filename=="")
  $filename=$php_self;
  $error=urlencode("youhavenoprivilegetoviewthispage!");
  header("location:login2.php?filename=$filename&error=$error&id=$id");
  exit(); #跳到无权用户登陆页面
  }
  ?>
  
    上述程序由 macro zeng保留完整解释权。
  
    


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons