您的位置:首页 > 编程语言 > PHP开发

PHP自动登录的实现和Cookie的安全性(UCHome的实现方法)

2015-06-24 22:37 736 查看
网站自动登录,即“记住我”、一周内免登录等,一般都是利用 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));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: