$this->meta charset utf 8[mt_rand(0,$_len)];这句是什么意思?

页面导航:
→ 正文内容 PHP验证码类
一个好用的PHP验证码类实例分享
这篇文章主要介绍了一个好用的PHP验证码类实例,有需要的朋友可以参考一下
分享一个好用的php验证码类,包括调用示例。说明:如果不适用指定的字体,那么就用imagestring()函数,如果需要遇到指定的字体,就要用到imagettftext()函数。字体的位置在C盘下Windows/Fonts.
参考了网上的php 生成验证码的方法,以及php 图片验证码和php 中文验证码的生成方法。用到了PHP GD库的相关知识。
1,生成验证码的类 VerificationCode.class.php 代码如下:&?php& &&& class VerificationCode{& &&&&&&& private $charset="abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";& //随机因子& &&&&&&& private $& //验证码& &&&&&&& private $codelen=4; //验证码长度& &&&&&&& private $width=110; //宽度& &&&&&&& private $height=30; //高度& &&&&&&& private $&& //图像资源句柄& &&&&&&& private $& //制定字体& &&&&&&& private $fontSize=25;&& //字体大小& &&&&&&& private $fontC //字体颜色& &&&&&&& public function __construct(){& &&&&&&&&&&& $this-&font="CALIBRIZ.TTF";& &&&&&&& }& &&&&&&& //生成验证码& &&&&&&& private function createCode(){& &&&&&&&&&&& $len=strlen($this-&charset)-1;& &&&&&&&&&&& for ($i = 0; $i & $this-& $i++) {& &&&&&&&&&&&&&&& $this-&code .= $this-&charset[mt_rand(0,$len)];& &&&&&&&&&&& }& &&&&&&& }& &&&&&&& //生成背景& &&&&&&& private function createBg(){& &&&&&&&&&&& $this-&img=imagecreatetruecolor($this-&width,$this-&height);& &&&&&&&&&&& $color = imagecolorallocate($this-&img,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255));& &&&&&&&&&&& imagefilledrectangle($this-&img,0,$this-&height,$this-&width,0,$color);& &&&&&&& }& &&&&&&& //生成文字& &&&&&&& private function createFont(){& &&&&&&&&&&& $x=$this-&width/$this-&& &&&&&&&&&&& for ($i = 0; $i & $this-& $i++) {& &&&&&&&&&&&&&&& $this-&fontColor=imagecolorallocate($this-&img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));& &&&&&&&&&&&&&&& imagettftext($this-&img,$this-&fontSize,mt_rand(-30,30),$i*$x+mt_rand(1,5),$this-&height/1.4,$this-&fontColor,$this-&font,$this-&code[$i]);& // www.jb51.net&&&&&&&&&&&&&&& //imagestring($this-&img,5,$i*$x+mt_rand(1,5),5,$this-&code[$i],$this-&fontColor);& &&&&&&&&&&& }& &&&&&&& }& &&&&&&& //生成线条、雪花& &&&&&&& private function createDisturb(){& &&&&&&&&&&& for ($i = 0; $i & 6; $i++) {& &&&&&&&&&&&&&&& $color=imagecolorallocate($this-&img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));& &&&&&&&&&&&&&&& imageline($this-&img,mt_rand(0,$this-&width),mt_rand(0,$this-&width),mt_rand(0,$this-&width),mt_rand(0,$this-&width),$color);& &&&&&&&&&&& }& &&&&&&&&&&& for ($i = 0; $i & 100; $i++) {& &&&&&&&&&&&&&&& $color=imagecolorallocate($this-&img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));& &&&&&&&&&&&&&&& imagestring($this-&img,mt_rand(1,5),mt_rand(0,$this-&width),mt_rand(0,$this-&height),'*',$color);& &&&&&&&&&&& }& &&&&&&& }& &&&&&&& //输出& &&&&&&& private function outPut(){& &&&&&&&&&&& header("Content-Type:image/png");& &&&&&&&&&&& imagepng($this-&img);& &&&&&&&&&&& imagedestroy($this-&img);& &&&&&&& }& &&&&&&& public function showCode(){& &&&&&&&&&&& $this-&createBg();& &&&&&&&&&&& $this-&createCode();& &&&&&&&&&&& $this-&createDisturb();& &&&&&&&&&&& $this-&createFont();& &&&&&&&&&&& $this-&outPut();& &&&&&&& }& &&&&&&& //获取验证码& &&&&&&& public function getCode(){& &&&&&&&&&&& return strtolower($this-&code);& &&&&&&& }& &&& }& ?&
代码如下:&?php& &&& session_start();& &&& require_once 'VerificationCode.class.php';& &&& $code=new VerificationCode();& &&& $_SESSION['code']=$code-&getCode();& &&& $code-&showCode();& ?&&
验证码:&input type="text" name="code" /&&img src="code.php" onclick="javascript:this.src='code.php?time='+Math.random();" /&
您可能感兴趣的文章:
上一篇:下一篇:
最 近 更 新
热 点 排 行
12345678910$pattern{mt_rand(0,7)};用{}括号包含,以前还没见过这样的写法_百度知道
$pattern{mt_rand(0,7)};用{}括号包含,以前还没见过这样的写法
,希望高手您解答一下;这句话算是什么意思的,以前还没见过这样的写法$pattern=&#39,用{}括号包含,7)};$pattern{mt_rand(0;
而上面的语句就是随机取其中一个值,mt_rand(0。$pattern是个字符串其实这个很简单。整个语句的意思就是取字符串$pattern中下标0-7(即从第一个到第八个)随机取一个字符。这个时候字符串就相当于一个字符数组,7)就是随机生成一个0-7的整数
其他类似问题
为您推荐:
rand的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁PHP 随机函数,彩票 $a=range(1,33); $b=array_rand($a,6); print_r($b); 为什么输出来的值会有0呢?_百度知道
PHP 随机函数,彩票 $a=range(1,33); $b=array_rand($a,6); print_r($b); 为什么输出来的值会有0呢?
.&quot,6);echo $a[$b[0]];大家看下为什么这二种输出结果会不同;echo $a[$b[3]];n&echo $a[$b[1]].&.&quot.&quot&\print_r($b);;n&quot,33);echo $a[$b[2]];\&#92?&$b=array_rand($a;;;n&n&.&echo $a[$b[5]];\\n&echo $a[$b[4]]。print_r的值中会出现 0
而ECHO 不会出现;?php$a=range(1
我有更好的答案
range(1,33)?
是啊?~你不知道这个函数?~
array_rand() 函数从数组中随机选出一个或多个元素,并返回。第二个参数用来确定要选出几个元素。如果选出的元素不止一个,则返回包含随机键名的数组),否则返回该元素的键名。(这是重点,貌似返回的是键名不是建值
是的,这可能是个bug
这个 array_rand 函数有问题,我之前做过无重复的4个数组也出现了0,只能自己在做个判断了
嗯 ~返回的结果中~ARRAY[0]=&0 会出现0~~我也觉得是值出现0真奇怪~因类我设置范围是(1.33)了的~最小的就是1 但是~用ECHO ARRAY[X] 出来的结果中没有0
改用mt_rand() 来处理随机, array_rand() 确实有问题
其他类似问题
为您推荐:
您可能关注的推广
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁12113人阅读
//验证码类
class ValidateCode {
private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ'; & &//随机因子
& & private $ & & & & & & & & & & & & & &//验证码
& & private $codelen = 4; & & & & & & & & & &//验证码长度
& & private $width = 130; & & & & & & & & & &//宽度
& & private $height = 50; & & & & & & & & & &//高度
& & private $ & & & & & & & & & & & & & & & &//图形资源句柄
& & private $ & & & & & & & & & & & & & & & &//指定的字体
& & private $fontsize = 20; & & & & & & & &//指定字体大小
& & private $ & & & & & & & & & & & &//指定字体颜色
& & //构造方法初始化
& & public function __construct() {
& & & & $this-&font = ROOT_PATH.'/font/elephant.ttf';
& & //生成随机码
& & private function createCode() {
& & & & $_len = strlen($this-&charset)-1;
& & & & for ($i=0;$i&$this-&$i++) {
& & & & & & $this-&code .= $this-&charset[mt_rand(0,$_len)];
& & //生成背景
& & private function createBg() {
& & & & $this-&img = imagecreatetruecolor($this-&width, $this-&height);
& & & & $color = imagecolorallocate($this-&img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
& & & & imagefilledrectangle($this-&img,0,$this-&height,$this-&width,0,$color);
& & //生成文字
& & private function createFont() { & &
& & & & $_x = $this-&width / $this-&
& & & & for ($i=0;$i&$this-&$i++) {
& & & & & & $this-&fontcolor = imagecolorallocate($this-&img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
& & & & & & imagettftext($this-&img,$this-&fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this-&height / 1.4,$this-&fontcolor,$this-&font,$this-&code[$i]);
& & //生成线条、雪花
& & private function createLine() {
& & & & for ($i=0;$i&6;$i++) {
& & & & & & $color = imagecolorallocate($this-&img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
& & & & & & imageline($this-&img,mt_rand(0,$this-&width),mt_rand(0,$this-&height),mt_rand(0,$this-&width),mt_rand(0,$this-&height),$color);
& & & & for ($i=0;$i&100;$i++) {
& & & & & & $color = imagecolorallocate($this-&img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
& & & & & & imagestring($this-&img,mt_rand(1,5),mt_rand(0,$this-&width),mt_rand(0,$this-&height),'*',$color);
& & //输出
& & private function outPut() {
& & & & header('Content-type:image/png');
& & & & imagepng($this-&img);
& & & & imagedestroy($this-&img);
& & //对外生成
& & public function doimg() {
& & & & $this-&createBg();
& & & & $this-&createCode();
& & & & $this-&createLine();
& & & & $this-&createFont();
& & & & $this-&outPut();
& & //获取验证码
& & public function getCode() {
& & & & return strtolower($this-&code);
&&&&&&& 输出实例:&
&&&&&&& 使用方法:
&&&&&&& 1、先把验证码类保存为一个名为 ValidateCode.class.php 的文件;
&&&&&&& 2、做一个名为 code.php 的文件进行调用该类;
& require './ValidateCode.class.php';
//先把类包含进来,实际路径根据实际情况进行修改。
& $_vc = new ValidateCode();
//实例化一个对象
& $_vc-&doimg();
& $_SESSION['code'] = $_vc-&getCode();//验证码保存到SESSION中
&&&&&&& 3、引用到页面中,代码如下:
&label&&img src=&./code.php& onclick=&javascript:this.src='./code.php?tm='+Math.random();& /&&/label&
声明:由于本人一开始写文章只为做笔记,因此,代码中存在遗漏和缺陷。特此致歉,并感谢大家的光临。
本文章的代码,已经整理。下载地址:
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:293488次
积分:4360
积分:4360
排名:第3015名
原创:138篇
转载:12篇
评论:119条
(1)(1)(6)(2)(1)(1)(9)(1)(5)(3)(6)(2)(1)(4)(7)(12)(9)(2)(6)(20)(20)(3)(16)(12)c语言中 a[i][j]=rand()/1000+1;是什么意思;随机发生函数rand()是怎么随机发生的,有什么范围吗??_百度知道
c语言中 a[i][j]=rand()/1000+1;是什么意思;随机发生函数rand()是怎么随机发生的,有什么范围吗??
提问者采纳
其中120是区间长度,125]的随机数;这句放在任何的rand()之前,可以这么写:int x = rand()%120 + 5. Use the srand function to seed the pseudorandom-number generator before calling rand,就是有可能在你的机器上运行.&rand()产生的伪随机数的范围是0到32767:#include &lt,一般想要产生比如[5.h&srand(time(NULL))。rand()产生的随机数是“伪随机数”,每次运行程序产生的随机数序列都是一样的MSDN中关于rand的描述&time,因此需要使用srand()解决这个问题;
//The rand function returns a pseudorandom integer in the range 0 to RAND_MAX (32767)
time(NULL)的作用是什么,srand()函数有什么用,rand()产生的随机数每次都是一样的吗 为什么会这样呢 那样还叫随机产生吗
这个跟计算机内部实现有关,所以rand()是“伪随机数”产生函数,注意,是“伪随机数”而不是“随机数”,如果每次srand()给的参数值相同,那么rand()产生的序列就是相同的。time(NULL)返回的是从某年的1月1日0时0分0秒开始(这个年份记不清了)到系统当前时间所经过的秒数,所以如果不是同一秒钟以内多次运行程序的话,time(NULL)的返回值一定是不同的,用这样的方式来产生相对比较随机的序列。
提问者评价
谢谢博士!
参考资料:
其他类似问题
为您推荐:
rand的相关知识
其他2条回答
也是最合理的做法:rand()%(a-b)+a;这样做是肯定不会出错的,相信你的智慧能马上想通 的?这样写是有很大的漏洞的,楼主你自己先好好想想谁教你这样写的!因为你不知道编译器提供的rand()产生随机数的范围!至于原理!所以除以1000是毫无头脑的做法!如果要产生a~b之间的随机数楼主你可以这样做
不是,前面还有程序,我也想不懂为什么要除1000,rand()%a+x我可以理解 程序是这样的main(){ int n,m,i,j,t,a[20][20]; printf(&请输入数字矩阵的行数及列数n,m:&); scanf(&%d,%d&,&n,&m); t=time(0)%1000;srand(t);//这里我想不明白什么意思。。。 for(i=1;i&=n;i++) {
printf(&\n&);
for(j=1;j&=m;j++)
a[i][j]=rand()/1000+1;
printf(&%4d&,a[i][j]);
} } printf(&\n&);}
t=time(0)%1000;srand(t);//这里我想不明白什么意思。。。这个是设置一个随机种子,你可以去百度百科查一下rand函数的定义,可能需要多看几遍才能看懂,那个解释非常详细的。
非常感谢哦
rand是取随机数的函数。你给出的语句意思就是说:a[i][j]这个数等于rand()这个函数产生的一个随机数,除以1000后再加1……
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 meta charset 的文章

 

随机推荐