阳光网驿-企业信息化交流平台【DTC零售连锁全渠道解决方案】

 找回密码
 注册

QQ登录

只需一步,快速开始

扫描二维码登录本站

手机号码,快捷登录

手机号码,快捷登录

老司机
查看: 1104|回复: 1

[转帖] PHP 实现 注册等的邮箱验证 (一)—— 加密函数 authcode

[复制链接]
  • TA的每日心情
    开心
    2021-8-30 00:00
  • 签到天数: 35 天

    [LV.5]常住居民I

    发表于 2014-12-14 23:22:07 | 显示全部楼层 |阅读模式
    邮箱验证的思路就是:发送一份包含激活链接的 EMAIL 到用户的邮箱中,这个激活链接 是通过GET方法传值,形如:

    1. http://XXXXX.com/activation.html?encode=asdasdwquqwe123jhu213hu
    复制代码
    在 activation.html 中接收 encode 的值,并对其验证。验证又可以分为 解密 这个加密字符串,又或者从数据库中查找是否有这个encode存在(那么在发这封邮件之前,应该将encode的值存在数据库中)。

    个人感觉 放到数据库中,思路简单一点,但是会对数据库进行查询操作,有点麻烦。我选择了第一种:加密之后,在接收的页面进行解密验证。可以选择 将 用户名 ,或者 email 加密,在 activation.html(激活页面) 将密文解密 获得 用户名,然后在数据库中 设定他 激活。

    我使用了 现成的一个函数 authcode  ,这个是 discuz 中的一个加,解密函数。 我感觉这个函数很不错,每次对同一个字符串加密后,密文都不一样,但是该函数可以正确解密。

    同时可以设定 密文 失效时间,准确到 秒数,这个正符合我们的要求,用户在规定时间里不点击链接,链接会自动失效!

    下面贴出该函数,网上讲解其原理的文章也有,我就不贴了,有时间研究下。。。。


    1. /**
    2. * @param string $string 原文或者密文
    3. * @param string $operation 操作(ENCODE | DECODE), 默认为 DECODE
    4. * @param string $key 密钥
    5. * @param int $expiry 密文有效期, 加密时候有效, 单位 秒,0 为永久有效
    6. * @return string 处理后的 原文或者 经过 base64_encode 处理后的密文
    7. *
    8. * @example
    9. *
    10. * $a = authcode('abc', 'ENCODE', 'key');
    11. * $b = authcode($a, 'DECODE', 'key');  // $b(abc)
    12. *
    13. * $a = authcode('abc', 'ENCODE', 'key', 3600);
    14. * $b = authcode('abc', 'DECODE', 'key'); // 在一个小时内,$b(abc),否则 $b 为空
    15. */
    16. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 3600) {
    17.         
    18.         $ckey_length = 4;
    19.         // 随机密钥长度 取值 0-32;
    20.         // 加入随机密钥,可以令密文无任何规律,即便是原文和密钥完全相同,加密结果也会每次不同,增大破解难度。
    21.         // 取值越大,密文变动规律越大,密文变化 = 16 的 $ckey_length 次方
    22.         // 当此值为 0 时,则不产生随机密钥
    23.         

    24.         $key = md5 ( $key ? $key : 'key' ); //这里可以填写默认key值
    25.         $keya = md5 ( substr ( $key, 0, 16 ) );
    26.         $keyb = md5 ( substr ( $key, 16, 16 ) );
    27.         $keyc = $ckey_length ? ($operation == 'DECODE' ? substr ( $string, 0, $ckey_length ) : substr ( md5 ( microtime () ), - $ckey_length )) : '';
    28.         
    29.         $cryptkey = $keya . md5 ( $keya . $keyc );
    30.         $key_length = strlen ( $cryptkey );
    31.         
    32.         $string = $operation == 'DECODE' ? base64_decode ( substr ( $string, $ckey_length ) ) : sprintf ( '%010d', $expiry ? $expiry + time () : 0 ) . substr ( md5 ( $string . $keyb ), 0, 16 ) . $string;
    33.         $string_length = strlen ( $string );
    34.         
    35.         $result = '';
    36.         $box = range ( 0, 255 );
    37.         
    38.         $rndkey = array ();
    39.         for($i = 0; $i <= 255; $i ++) {
    40.                 $rndkey [$i] = ord ( $cryptkey [$i % $key_length] );
    41.         }
    42.         
    43.         for($j = $i = 0; $i < 256; $i ++) {
    44.                 $j = ($j + $box [$i] + $rndkey [$i]) % 256;
    45.                 $tmp = $box [$i];
    46.                 $box [$i] = $box [$j];
    47.                 $box [$j] = $tmp;
    48.         }
    49.         
    50.         for($a = $j = $i = 0; $i < $string_length; $i ++) {
    51.                 $a = ($a + 1) % 256;
    52.                 $j = ($j + $box [$a]) % 256;
    53.                 $tmp = $box [$a];
    54.                 $box [$a] = $box [$j];
    55.                 $box [$j] = $tmp;
    56.                 $result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );
    57.         }
    58.         
    59.         if ($operation == 'DECODE') {
    60.                 if ((substr ( $result, 0, 10 ) == 0 || substr ( $result, 0, 10 ) - time () > 0) && substr ( $result, 10, 16 ) == substr ( md5 ( substr ( $result, 26 ) . $keyb ), 0, 16 )) {
    61.                         return substr ( $result, 26 );
    62.                 } else {
    63.                         return '';
    64.                 }
    65.         } else {
    66.                 return $keyc . str_replace ( '=', '', base64_encode ( $result ) );
    67.         }

    68. }
    复制代码
    1. $a = authcode('abc', 'ENCODE', 'key');
    2. $b = authcode($a, 'DECODE', 'key');  // $b(abc)

    3. //其中的 'key' 可以换成 你想要的安全字符串,比如'SAFE@31#'之类的,值的注意的是 :加密时用的 ‘key’ 解密时必须一致!如:
    4. //加密:
    5. $a = authcode('abc', 'ENCODE', 'safe@@tt');
    6. //解密:
    7. $b = authcode($a, 'DECODE', 'safe@@tt');  // $b(abc)

    8. //这样有很大的 灵活性 ,同时也很安全!

    9. //设定 过期时间 (加密的时候设定!)
    10. $a = authcode('abc', 'ENCODE', 'key', 3600);
    11. $b = authcode('abc', 'DECODE', 'key'); // 在一个小时内,$b(abc),否则 $b 为 空
    复制代码


    值得 注意的是, 该函数加密的密文 不能直接通过  URL 传值,这是为什么呢??

    因为 该函数 加密的密文 包含 ‘+’ ‘/’ ‘|’  等特殊字符,放到  URL 中会发生错误。我们通过 URL 传值,必须将这些 特殊字符 换成 URL 编码 ,这里使用PHP 的一个函数
    1. urlencode  ()

    2. 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>.
    复制代码

    呵呵 手册就这样介绍的哇

    下面简单的例子:
    1. $safeEmail= authcode ( $email, 'ENCODE', 'safeEmail', 3600 );
    2. $urlEnSafeEmail = urlencode ( $safeEmail );

    3. /*链接 */
    4. $link = "<a href = 'http://www.XXX.com/activation?code=$urlEnSafeEmail'>http://www.XXX.com/activation.html?code=$urlEnSafeEmail</a>";
    5.        

    6. //在 activation.html 页面
    7. $code = @$_GET ['code'];
    8. $email = authcode ( $code, 'DECODE', 'safeEmail' );
    9. $user = findUserByEmail ( $email );//  对数据库 查找操作,视情况而定
    10. if (!$user) {
    11.         exit('非法操作');
    12. }
    13. echo "<script>alert('激活成功!');location.href='http://www.XXX.com/welcome.html'</script>";
    复制代码

    该贴已经同步到 sunwy的微博
    楼主热帖
    启用邀请码注册,提高发帖质量,建设交流社区
  • TA的每日心情

    2024-5-23 09:42
  • 签到天数: 139 天

    [LV.7]常住居民III

    发表于 2014-12-19 14:58:11 | 显示全部楼层
    收藏了
    启用邀请码注册,提高发帖质量,建设交流社区
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    快速回复 返回顶部 返回列表