邮箱验证的思路就是:发送一份包含激活链接的 EMAIL 到用户的邮箱中,这个激活链接 是通过GET方法传值,形如:
- http://XXXXX.com/activation.html?encode=asdasdwquqwe123jhu213hu
复制代码 在 activation.html 中接收 encode 的值,并对其验证。验证又可以分为 解密 这个加密字符串,又或者从数据库中查找是否有这个encode存在(那么在发这封邮件之前,应该将encode的值存在数据库中)。
个人感觉 放到数据库中,思路简单一点,但是会对数据库进行查询操作,有点麻烦。我选择了第一种:加密之后,在接收的页面进行解密验证。可以选择 将 用户名 ,或者 email 加密,在 activation.html(激活页面) 将密文解密 获得 用户名,然后在数据库中 设定他 激活。
我使用了 现成的一个函数 authcode ,这个是 discuz 中的一个加,解密函数。 我感觉这个函数很不错,每次对同一个字符串加密后,密文都不一样,但是该函数可以正确解密。
同时可以设定 密文 失效时间,准确到 秒数,这个正符合我们的要求,用户在规定时间里不点击链接,链接会自动失效!
下面贴出该函数,网上讲解其原理的文章也有,我就不贴了,有时间研究下。。。。
- /**
- * @param string $string 原文或者密文
- * @param string $operation 操作(ENCODE | DECODE), 默认为 DECODE
- * @param string $key 密钥
- * @param int $expiry 密文有效期, 加密时候有效, 单位 秒,0 为永久有效
- * @return string 处理后的 原文或者 经过 base64_encode 处理后的密文
- *
- * @example
- *
- * $a = authcode('abc', 'ENCODE', 'key');
- * $b = authcode($a, 'DECODE', 'key'); // $b(abc)
- *
- * $a = authcode('abc', 'ENCODE', 'key', 3600);
- * $b = authcode('abc', 'DECODE', 'key'); // 在一个小时内,$b(abc),否则 $b 为空
- */
- function authcode($string, $operation = 'DECODE', $key = '', $expiry = 3600) {
-
- $ckey_length = 4;
- // 随机密钥长度 取值 0-32;
- // 加入随机密钥,可以令密文无任何规律,即便是原文和密钥完全相同,加密结果也会每次不同,增大破解难度。
- // 取值越大,密文变动规律越大,密文变化 = 16 的 $ckey_length 次方
- // 当此值为 0 时,则不产生随机密钥
-
- $key = md5 ( $key ? $key : 'key' ); //这里可以填写默认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 );
-
- $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 ) );
- }
- }
复制代码- $a = authcode('abc', 'ENCODE', 'key');
- $b = authcode($a, 'DECODE', 'key'); // $b(abc)
- //其中的 'key' 可以换成 你想要的安全字符串,比如'SAFE@31#'之类的,值的注意的是 :加密时用的 ‘key’ 解密时必须一致!如:
- //加密:
- $a = authcode('abc', 'ENCODE', 'safe@@tt');
- //解密:
- $b = authcode($a, 'DECODE', 'safe@@tt'); // $b(abc)
- //这样有很大的 灵活性 ,同时也很安全!
- //设定 过期时间 (加密的时候设定!)
- $a = authcode('abc', 'ENCODE', 'key', 3600);
- $b = authcode('abc', 'DECODE', 'key'); // 在一个小时内,$b(abc),否则 $b 为 空
复制代码
值得 注意的是, 该函数加密的密文 不能直接通过 URL 传值,这是为什么呢??
因为 该函数 加密的密文 包含 ‘+’ ‘/’ ‘|’ 等特殊字符,放到 URL 中会发生错误。我们通过 URL 传值,必须将这些 特殊字符 换成 URL 编码 ,这里使用PHP 的一个函数- urlencode ()
- This function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to <span style="color:#ff00;">pass variables to the next page</span>.
复制代码
呵呵 手册就这样介绍的哇
下面简单的例子:
- $safeEmail= authcode ( $email, 'ENCODE', 'safeEmail', 3600 );
- $urlEnSafeEmail = urlencode ( $safeEmail );
- /*链接 */
- $link = "<a href = 'http://www.XXX.com/activation?code=$urlEnSafeEmail'>http://www.XXX.com/activation.html?code=$urlEnSafeEmail</a>";
-
- //在 activation.html 页面
- $code = @$_GET ['code'];
- $email = authcode ( $code, 'DECODE', 'safeEmail' );
- $user = findUserByEmail ( $email );// 对数据库 查找操作,视情况而定
- if (!$user) {
- exit('非法操作');
- }
- echo "<script>alert('激活成功!');location.href='http://www.XXX.com/welcome.html'</script>";
复制代码
该贴已经同步到 sunwy的微博
|