Home >php教程 >php手册 >php的安全字符串类

php的安全字符串类

WBOY
WBOYOriginal
2016-06-06 19:59:031462browse

写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

<?php /**
  *
  * @title  安全字符串类
  * @author rainysia
  * @version 1.0.0
  * @copyright rainysia, 2012-07-05 18:47:13 星期四
  * @package default
  * @link_me 
  *
  *
  **/


/**
 *  @产生随机字符类
 *  @参数: len 为自定义的字符长度
 *  @使用: 实例化该类既可
 *  $www = new genRandomString();
 *  $www->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

<?php if(!isset($_COOKIE['WEB_TITLE'])){
					$cookievalue = new genRandomString();
					$cookievalue->len='20';
					$s = $cookievalue->provideRandString();
					setcookie("WEB",$s,time()+10803,"/") or die ("客户端禁止cookie");
				}else{
					echo "欢迎回来";
				}
				?>
其中WEB_TITLE是在网站的配置文件config.ini.php中定义的 (define )

登录时产生一个cookie文件.里面的$value即为这个类生成的.


php的安全字符串类

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn