php用戶登錄之cookie信息安全分析
本文實例講述了php用戶登錄之cookie信息安全。分享給大家供大家參考,具體如下:
大家都知道用戶登陸后,用戶信息一般會選擇保存在cookie里面,因為cookie是保存客戶端,并且cookie可以在客戶端用瀏覽器自由更改,這樣將會造成用戶cookie存在偽造的危險,從而可能使偽造cookie者登錄任意用戶的賬戶。
下面就說說平常一些防止用戶登錄cookie信息安全的方法:
一、cookie信息加密法
cookie信息加密法即用一種加密方法,加密用戶信息,然后在存入cookie,這樣偽造者即使得到cookie也只能在cookie有效期內(nèi)對這個cookie利用,無法另外偽造cookie信息。
這里附上一個加密函數(shù):
<?php function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) { // 動態(tài)密匙長度,相同的明文會生成不同密文就是依靠動態(tài)密匙 $ckey_length = 4; // 密匙 $key = md5($key ? $key : $GLOBALS['discuz_auth_key']); // 密匙a會參與加解密 $keya = md5(substr($key, 0, 16)); // 密匙b會用來做數(shù)據(jù)完整性驗證 $keyb = md5(substr($key, 16, 16)); // 密匙c用于變化生成的密文 $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : ''; // 參與運算的密匙 $cryptkey = $keya.md5($keya.$keyc); $key_length = strlen($cryptkey); // 明文,前10位用來保存時間戳,解密時驗證數(shù)據(jù)有效性,10到26位用來保存$keyb(密匙b), //解密時會通過這個密匙驗證數(shù)據(jù)完整性 // 如果是解碼的話,會從第$ckey_length位開始,因為密文前$ckey_length位保存 動態(tài)密匙,以保證解密正確 $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string; $string_length = strlen($string); $result = ''; $box = range(0, 255); $rndkey = array(); // 產(chǎn)生密匙簿 for($i = 0; $i <= 255; $i++) { $rndkey[$i] = ord($cryptkey[$i % $key_length]); } // 用固定的算法,打亂密匙簿,增加隨機性,好像很復雜,實際上對并不會增加密文的強度 for($j = $i = 0; $i < 256; $i++) { $j = ($j + $box[$i] + $rndkey[$i]) % 256; $tmp = $box[$i]; $box[$i] = $box[$j]; $box[$j] = $tmp; } // 核心加解密部分 for($a = $j = $i = 0; $i < $string_length; $i++) { $a = ($a + 1) % 256; $j = ($j + $box[$a]) % 256; $tmp = $box[$a]; $box[$a] = $box[$j]; $box[$j] = $tmp; // 從密匙簿得出密匙進行異或,再轉(zhuǎn)成字符 $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256])); } if($operation == 'DECODE') { // 驗證數(shù)據(jù)有效性,請看未加密明文的格式 if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() --> 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) { return substr($result, 26); } else { return ''; } } else { // 把動態(tài)密匙保存在密文里,這也是為什么同樣的明文,生產(chǎn)不同密文后能解密的原因 // 因為加密后的密文可能是一些特殊字符,復制過程可能會丟失,所以用base64編碼 return $keyc.str_replace('=', '', base64_encode($result)); } } $str = 'abcdef'; $key = 'chabaoo.cn'; echo $jm = authcode($str,'ENCODE',$key,0); //加密 echo " "; echo authcode($jm ,'DECODE',$key,0); //解密 ?>
這樣當設置用戶信息的cookie時,就無法對其進行偽造:
<?php $user = array("uid"=-->$uid,"username"=>$username); $user = base64_encode(serialize($user)); $user = authcode($user,'ENCODE','chabaoo.cn',0); //加密 setcookie("user",$user,time()+3600*24); ?>
二、用加密令牌對cookie進行保護
$hash = md5($uid.time());//加密令牌值 $hash_expire =time()+3600*24;//加密令牌值為一天有效期 $user = array("uid"=>$uid,"username"=>$username,"hash"=>$hash); $user = base64_encode(serialize($user)); setcookie("user",$user,$hash_expr);
然后把$hash和$hash_expire 存入member表中hash和hash_expire對應字段中,也可以存入nosql,session
用戶偽造cookie時,hash無法偽造,偽造的hash和數(shù)據(jù)庫中的不一致
用戶每次登陸,這個hash_expire有效期內(nèi)不更新hash值,過期則更新
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php curl用法總結(jié)》、《PHP運算與運算符用法總結(jié)》、《PHP網(wǎng)絡編程技巧總結(jié)》、《PHP基本語法入門教程》、《php操作office文檔技巧總結(jié)(包括word,excel,access,ppt)》、《php日期與時間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
相關(guān)文章
php從數(shù)據(jù)庫查詢結(jié)果生成樹形列表的方法
這篇文章主要介紹了php從數(shù)據(jù)庫查詢結(jié)果生成樹形列表的方法,涉及php操作html元素生成樹形列表的技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04PHP采用XML-RPC構(gòu)造Web Service實例教程
這篇文章主要介紹了PHP采用XML-RPC構(gòu)造Web Service,需要的朋友可以參考下2014-07-07PHP判斷FORM表單或URL參數(shù)來的數(shù)據(jù)是否為整數(shù)的方法
這篇文章主要介紹了PHP判斷FORM表單或URL參數(shù)來的數(shù)據(jù)是否為整數(shù)的方法,需要的朋友可以參考下2016-03-03修復ShopNC使用QQ 互聯(lián)時提示100010 錯誤
本文給大家介紹了修復ShopNC使用QQ 互聯(lián)時提示100010 錯誤的方法,以及QQ互聯(lián)里面的處理方法,有需要的小伙伴可以參考下2015-11-11php批量添加數(shù)據(jù)與批量更新數(shù)據(jù)的實現(xiàn)方法
這篇文章主要介紹了php批量添加數(shù)據(jù)與批量更新數(shù)據(jù)的實現(xiàn)方法,涉及針對表單的處理與sql語句的靈活使用,非常具有實用價值,需要的朋友可以參考下2014-12-12