1. 程式人生 > >PHP自動登入的實現和Cookie的安全性(UCHome的實現方法)

PHP自動登入的實現和Cookie的安全性(UCHome的實現方法)

網站自動登入,即“記住我”、一週內免登入等,一般都是利用 cookie 來實現的。cookie 儲存了使用者名稱和密碼等資訊,再次訪問時,將 cookie 資料查詢資料庫對比。

使用者名稱可以直接儲存到 cookie裡,而密碼必須經過加密處理,並保證加密演算法難以破解、且能夠解密。

本文參考 UCHome 自動登入的實現方法,並利用 UCHome 的加密解密函式 authcode(),將自動登入的過程整理出來。這裡沒有完整的程式碼,僅僅是簡單的思路。

第一:登入表單頁面 login.html

<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
<meta charset="utf-8" />
<title>使用者登入</title>
<style>
*{padding: 0;margin: 0;}
body{padding:20px;}
</style>
</head>
<body>
<form action="login.php" method="POST">
	<label>使用者名稱:<input type="text" name="username" /></label><br />
	<label>密碼:<input type="password" name="password" /></label><br />
	<label><input type="checkbox" name="cookietime" value="864000" />十天內免登入</label>
	<input type="submit" value="登入" />
</form>
</body>
</html>

第二、提交到 login.php 頁面

<?php

//金鑰
define('UC_KEY', '123456');

//表單提交的資料
$password = $_POST['password'];
$username = trim($_POST['username']);
$cookietime = intval($_POST['cookietime']);

//查詢資料庫,登入成功
//假設登入成功後,獲得到的使用者資訊為 $row
$row = array('username'=>'admin', 'password'=>'21232f297a57a5a743894a0e4a801fc3', 'uid'=>1);

/**
 * 登入成功,設定 cookie
 * (1) authcode 函式附在文章最後;
 * (2) UCHome 是將 "密碼+製表符+使用者id" 進行加密
*/
setcookie('username', $row['username'], time() + $cookietime);
setcookie('auth', authcode("$row['password']\t$row['uid']", "ENCODE"), time()+$cookietime);

第三、再次訪問網站時,解密 $_COOKIE['auth'],實現自動登入

//金鑰
define('UC_KEY', '123456');

// $_COOKIE['auth'] 解密
// 得到密碼($password)和使用者uid($uid)
@list($password, $uid) = explode("\t", authcode($_COOKIE['auth'], 'DECODE'));

// 根據 uid 查詢資料庫
$row = mysql_query('select * from member表 where uid=' . $uid);

//對比密碼是否相等
if($row['password'] == $password){
	//設定session,實現自動登入
}

附:UCHome 的加密解密函式 authcode()

//字串解密加密
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {

	// 隨機金鑰長度 取值 0-32;
	// 加入隨機金鑰,可以令密文無任何規律,即便是原文和金鑰完全相同,加密結果也會每次不同,增大破解難度。
	// 取值越大,密文變動規律越大,密文變化 = 16 的 $ckey_length 次方
	// 當此值為 0 時,則不產生隨機金鑰
	$ckey_length = 4;

	$key = md5($key ? $key : UC_KEY);
	$keya = md5(substr($key, 0, 16));
	$keyb = md5(substr($key, 16, 16));
	$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';

	$cryptkey = $keya.md5($keya.$keyc);
	$key_length = strlen($cryptkey);

	echo $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 {
		return $keyc.str_replace('=', '', base64_encode($result));
	}
}