php使用者登入之cookie資訊安全的用法及執行個體詳解

來源:互聯網
上載者:User
這篇文章主要介紹了php使用者登入之cookie資訊安全,介紹了cookie加密與令牌保護兩種cookie資訊安全保護的技巧,需要的朋友可以參考下

大家都知道使用者登陸後,使用者資訊一般會選擇儲存在cookie裡面,因為cookie是儲存用戶端,並且cookie可以在用戶端用瀏覽器自由更改,這樣將會造成使用者cookie存在偽造的危險,從而可能使偽造cookie者登入任意使用者的賬戶。

下面就說說平常一些防止使用者登入cookie資訊安全的方法:

一、cookie資訊加密法

cookie資訊加密法即用一種加密方法,加密使用者資訊,然後在存入cookie,這樣偽造者即使得到cookie也只能在cookie有效期間內對這個cookie利用,無法另外偽造cookie資訊。

這裡附上一個加密函數:

<?phpfunction authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {  // 動態密匙長度,相同的明文會產生不同密文就是依靠動態密匙  $ckey_length = 4;  // 密匙  $key = md5($key ? $key : $GLOBALS['discuz_auth_key']);  // 密匙a會參與加解密  $keya = md5(substr($key, 0, 16));  // 密匙b會用來做資料完整性驗證  $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位用來儲存時間戳記,解密時驗證資料有效性,10到26位用來儲存$keyb(密匙b),//解密時會通過這個密匙驗證資料完整性  // 如果是解碼的話,會從第$ckey_length位開始,因為密文前$ckey_length位儲存 動態密匙,以保證解密正確  $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();  // 產生密匙簿  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;    // 從密匙簿得出密匙進行異或,再轉成字元    $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));  }  if($operation == 'DECODE') {    // 驗證資料有效性,請看未加密明文的格式    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 {    // 把動態密匙儲存在密文裡,這也是為什麼同樣的明文,生產不同密文後能解密的原因    // 因為加密後的密文可能是一些特殊字元,複製過程可能會丟失,所以用base64編碼    return $keyc.str_replace('=', '', base64_encode($result));  }}$str = 'abcdef';$key = 'www.jb51.net';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','www.jb51.net',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和資料庫中的不一致

使用者每次登陸,這個hash_expire有效期間內不更新hash值,到期則更新

總結:以上就是本篇文的全部內容,希望能對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.