php的安全字符串类
程序员文章站
2022-05-07 18:25:23
...
写cookie的value的时候,看了下google和baidu的cookie文件,里面的value是一长串字符.于是写了个安全字符串类来产生安全字符串. 这个是security.php ?php/** * * @title 安全字符串类 * @author rainysia * @version 1.0.0 * @copyright rainysia, 2012-07-05
写cookie的value值的时候,看了下google和baidu的cookie文件,里面的value是一长串字符.于是写了个安全字符串类来产生安全字符串.
这个是security.php
len='40'; * echo $www->provideRandString(); * unset($www); */ class genRandomString{ public function provideRandString(){ $chars = array('a','b','c','d','e','f','g','h','j','k','m','n','p','q','r','s','t','u','v','w','x','y','z','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z','_','#','@','%','&','!'); $charsLen = count($chars) - 1; shuffle($chars); $grs = ""; $len = ""; for ($i = 0; $i len; $i++) { $grs .= $chars[mt_rand(0,$charsLen)]; } return $grs; } } ?>在登录页面index.php中,我们先include '../lib/security.php';
然后在登录页面的提交按钮后,写这样一段php
len='20'; $s = $cookievalue->provideRandString(); setcookie("WEB",$s,time()+10803,"/") or die ("客户端禁止cookie"); }else{ echo "欢迎回来"; } ?>其中WEB_TITLE是在网站的配置文件config.ini.php中定义的 (define )
登录时产生一个cookie文件.里面的$value即为这个类生成的.
上一篇: 解放web程序员的输入验证_表单特效