用Session對(duì)Web頁(yè)面進(jìn)行保護(hù)
2024-05-04 22:53:43
供稿:網(wǎng)友
 
在很多時(shí)候,我們都要對(duì)某些web 頁(yè)面進(jìn)行安全保護(hù)。典型的例子就是前臺(tái)瀏覽頁(yè)面與后臺(tái)管理頁(yè)面的安全性。這也是 web 上用得最多的一種頁(yè)面安全模式。在用php4 開(kāi)發(fā)一個(gè)小型的書籍管理系統(tǒng)中,我也遇到了這個(gè)安全問(wèn)題。于是我想到了php4 的新特性----session .. 
 
 要求目的:同一站點(diǎn),無(wú)權(quán)用戶,一般授權(quán)用戶和超級(jí)用戶能看到和使用不同的頁(yè)面。 
 
 實(shí)現(xiàn)辦法:在要保護(hù)的頁(yè)面 include 不同級(jí)別的安全檢驗(yàn)摸板。 
 
 注意事項(xiàng): 
 1 > 要避免用戶瀏覽器不使用 cookie 而不能瀏覽受保護(hù)的頁(yè)面(session 默認(rèn)使用客戶端的 cookie). 
 2 > 要防止權(quán)限被盜用。( php 4 的 session 的默認(rèn)存活期間是從建立 session 開(kāi)始到關(guān)閉瀏覽器為止。) 
 
 如何使用: 
 1 > 在需要一般保護(hù)的頁(yè)面的代碼最前邊加上 include ("secturity2.php"); 就行了 
 2 > 在需要特殊保護(hù)的頁(yè)面的代碼最前邊加上 include ("secturity1.php"); 和 include ("secturity2.php"); 就行了 
 (假設(shè)所有文件都在同一個(gè)文件夾里) 
 
 程序代碼及詳細(xì)解釋: 
 security1.php 特殊用戶頁(yè)面保護(hù)摸板 
 security2.php 一般用戶頁(yè)面保護(hù)摸板 
 login2.php 用戶登陸頁(yè)面 
 
 我們先來(lái)看 login2.php (用戶登陸頁(yè)面)的代碼: 
 
 <?php 
 session_register("user");#增加用戶名變數(shù) 
 session_register("password"); #增加密碼變數(shù) 
 session_register("tmlast"); #增加時(shí)間變數(shù) 
 if($user==""){#判斷是否是第一次登陸 
 $error="chooseyounameandinputthepasswordplease!"; 
 } 
 $tmlast=date("u"); #記錄登陸時(shí)間 
 if($user1) 
 $user=trim($user1); #記錄用戶名(引用user1變量是為什么?請(qǐng)讀者自己思考。) 
 $password=trim($password1); #記錄密碼 
 if($user1&&$password1){ 
 if($password1==888){ #判斷登陸密碼是否是默認(rèn)密碼888結(jié)束 php 程式 
 $sid="phpsessid=".session_id(); #保存當(dāng)前session的id號(hào) 
 $warning="yourpasswordisstillthedefaultpassword888,pleasechangeit."; 
 header("location:changepassword.php?$sid&warning=$warning"); #傳遞警告參數(shù)warning到changepassword.php 頁(yè)面 
 exit(); #立刻結(jié)束 php 程式 
 } 
 if(strtolower($user)=="root"){ #判斷登陸用戶是否是超級(jí)用戶,可以自行擴(kuò)充用戶 
 $filename="backend_index.php"; 
 } 
 else{ 
 if(!$filename) #判斷進(jìn)入登陸頁(yè)面的上一頁(yè)是否是受保護(hù)頁(yè)面 
 $filename="index.php"; 
 } 
 $sid="phpsessid=".session_id(); #保存當(dāng)前session的id號(hào) 
 header("location:$filename?$sid); #登陸成功進(jìn)入指定頁(yè)面,傳遞當(dāng)前session的id號(hào),防止用戶不使用 cookie 而讀不到 session 值 
 exit(); #立刻結(jié)束 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;#提交到當(dāng)前頁(yè)?>"method=post> 
 <p><b>name:</b> 
 <?php 
 include("class/dbclass.inc"); #調(diào)用dbclass.inc類,用法和 mysql.inc 類一樣 
 $q=newdb_sql; #定義一個(gè)新的對(duì)象 
 $q->connect($host,$database,$user,$password); #連接 mysql數(shù)據(jù)庫(kù) 
 $query="selectchrusername,chrfirstname,chrlastname". 
 "fromuser". 
 "wherechrfirstname!=''". 
 "orderbychrfirstname"; 
 $q->query($query); #執(zhí)行sql語(yǔ)句 
 echo"<selectname=user1size=1>"; 
 while($q->next_record()){ #從數(shù)據(jù)庫(kù)中調(diào)出一般用戶 
 if($user==$q->f(0)) #判斷是否是當(dāng)前用戶 
 $select="selected"; #是當(dāng)前用戶則設(shè)置為默認(rèn)值 
 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=確認(rèn)></p> 
 </form> 
 
 security2.php (一般用戶頁(yè)面保護(hù)摸板): 
 <?php 
 session_register("user"); #說(shuō)明同上 
 session_register("password"); 
 session_register("tmlast"); 
 if($filename=="") 
 $filename=$php_self; #記錄當(dāng)前頁(yè)面路徑 
 if($durtime=="") 
 $durtime=300; #設(shè)置 session “失效”時(shí)間 
 $currtime=date("u"); 
 if(($currtime-$tmlast)>$durtime){ #判斷 session是否“失效” 
 //session_destroy(); 
 $error=urlencode("seesionexpired.loginagainplease!"); 
 header("location:login2.php?filename=$filename&error=$error&user=$user"); #跳到重新登陸頁(yè) 
 exit(); 
 } 
 else{ 
 $tmlast=$currtime; # session 沒(méi)“失效”則更新最后“登陸”時(shí)間 
 } 
 
 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"); #跳到密碼錯(cuò)誤登陸頁(yè) 
 } 
 else{ 
 $sid="phpsessid=".session_id(); 
 $q->next_record(); 
 $userid=$q->f(iduser); #保存通過(guò)驗(yàn)證用戶的id號(hào),方便以后使用 
 } 
 ?> 
 
 security1.php (特殊用戶頁(yè)面保護(hù)摸板): 
 <?php 
 session_register("user"); #說(shuō)明同上 
 $privilege="root,macro,jackie"; #設(shè)置超級(jí)用戶名單列表,用“,”隔開(kāi) 
 $pieces=explode(",",$privilege); #取得單個(gè)超級(jí)用戶名單 
 for($i=0;$i<count($pieces);$i++){ 
 if(strtolower($user)==$pieces[$i]){ #判斷是否是超級(jí)用戶 
 $hasprivilege=1; 
 break; #跳出判斷循環(huán) 
 } 
 } 
 
 if(!$hasprivilege){ 
 if($filename=="") 
 $filename=$php_self; 
 $error=urlencode("youhavenoprivilegetoviewthispage!"); 
 header("location:login2.php?filename=$filename&error=$error&id=$id"); 
 exit(); #跳到無(wú)權(quán)用戶登陸頁(yè)面 
 } 
 ?>