⑴ php验证码判断问题
没明白你的意思, 验证码就是这样, 生成的时候将验证码写进session, 验证的时候取出来的肯定是上次生成那个.
能详细说一下你哪里不明白么?
------------------------------
我想你是没明白验证码的工作原理, 验证码多数情况是用来防止机器人自动发贴或注册而采取的一种验证手段, 在php中验证码多数用session来实现.
举个例子让你理解下流程.
1. 用户访问注册页面a.php
2. a.php输出了一个验证码b.php,实际上等于用户向b.php发送了一个请求.
3. b.php生成一个随机数$a, 并将这个随机数保存到session中, 然后将图片显示给用户
4. 用户填写注册表单并提交到c.php
5. c.php接受用户注册信息, 但在注册之前要从session中取出随机数$a和你提交上来的验证码做一下比较, 这个随机数$a是在b.php那个请求中生成的, 也就是在a.php中看到的那个图片. 比较如果相同那么同意你进行申请, 如果不相同则返回错误信息并跳转回a.php, 然后回到步骤1
这里我觉得你最应该了解的是session的作用,是用来在会话期间内在请求之间传递数据.
⑵ 怎么让php生成的验证码随机数不重复
如果要生成四位数字的验证码,则可以用函数: $srand = rand(1000,9999); 会生成在1000到9999之间的随机数字,如果要生成更多位数的数字,可以更改最孝最大值。
⑶ php验证码是保存在数据库中还是直接在session中进行判断的
我不明白你的意思,验证码时产生的验证码写入会话,认证肯定取出,最后产生。
细节,你不明白是什么?
------------------------------
我想以在大多数情况下,你不明白的工作原理验证码,验证码是用来防止机器人自动发贴或注册身份验证手段,达到最在PHP会话中使用验证码。
例如,你理解下面的过程。
用户进入注册页面a.php中
2。 a.php中输出验证码的b.php中,实际上等于用户b.php中发送一个请求。
3 b.php中生成一个随机数$ a,和随机数被保存在会话中,画面,然后显示给用户
用户填写登记表,并提交c.php
5的c.php接受用户的注册信息,但在登记前从会话中删除随机数验证码做一些比较,随机数您提交b.php的要求,那就是,在生成a.php中看到图片。同意你的应用程序,如果不相同则返回错误消息跳回a.php中,然后返回到相同的,所以第1步
我想你应该知道,本届会议的作用,是用来传输数据之间的会话期间的要求。
⑷ 如何用PHP生成验证码
PHP生成验证码的原理:使用PHP的GD库,生成一张带验证码的图片,并将验证码保存在Session中。PHP生成验证码的大致流程有:
1、产生一张png的图片;
2、为图片设置背景色;
3、设置字体颜色和样式;
4、产生4位数的随机的验证码;
5、把产生的每个字符调整旋转角度和位置画到png图片上;
6、加入噪点和干扰线防止注册机器分析原图片来恶意破解验证码;
7、输出图片;
8、释放图片所占内存。
session_start();
getCode(4,60,20);
functiongetCode($num,$w,$h){
$code="";
for($i=0;$i<$num;$i++){
$code.=rand(0,9);
}
//4位验证码也可以用rand(1000,9999)直接生成
//将生成的验证码写入session,备验证时用
$_SESSION["helloweba_num"]=$code;
//创建图片,定义颜色值
header("Content-type:image/PNG");
$im=imagecreate($w,$h);
$black=imagecolorallocate($im,0,0,0);
$gray=imagecolorallocate($im,200,200,200);
$bgcolor=imagecolorallocate($im,255,255,255);
//填充背景
imagefill($im,0,0,$gray);
//画边框
imagerectangle($im,0,0,$w-1,$h-1,$black);
//随机绘制两条虚线,起干扰作用
$style=array($black,$black,$black,$black,$black,
$gray,$gray,$gray,$gray,$gray
);
imagesetstyle($im,$style);
$y1=rand(0,$h);
$y2=rand(0,$h);
$y3=rand(0,$h);
$y4=rand(0,$h);
imageline($im,0,$y1,$w,$y3,IMG_COLOR_STYLED);
imageline($im,0,$y2,$w,$y4,IMG_COLOR_STYLED);
//在画布上随机生成大量黑点,起干扰作用;
for($i=0;$i<80;$i++){
imagesetpixel($im,rand(0,$w),rand(0,$h),$black);
}
//将数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成
$strx=rand(3,8);
for($i=0;$i<$num;$i++){
$strpos=rand(1,6);
imagestring($im,5,$strx,$strpos,substr($code,$i,1),$black);
$strx+=rand(8,12);
}
imagepng($im);//输出图片
imagedestroy($im);//释放图片所占内存
}
⑸ php中如何使用随机函数rand()生成一个数字验证码
如果要生成四位数字的验证码,则可以用函数:
$srand = rand(1000,9999);
会生成在1000到9999之间的随机数字,如果要生成更多位数的数字,可以更改最小、最大值。
⑹ PHP怎么生成4位随机纯数字重复几率要万分之一以上!怎么弄啊,急!
如果要生成四位数字的验证码,则可以用函数:$srand=rand(1000,9999);会生成在1000到9999之间的随机数字,如果要生成位数的数字,可以更改最孝最大值。
⑺ php的图片验证码代码
这个是phpcms的验证码,经过十几万个网站经验的,非常好用
<?php
session_start();
$enablegd = 1;
//判断图像处理函数是否存在
$funcs = array('imagecreatetruecolor','imagecolorallocate','imagefill','imagestring','imageline','imagerotate','imagedestroy','imagecolorallocatealpha','imageellipse','imagepng');
foreach($funcs as $func)
{
if(!function_exists($func))
{
$enablegd = 0;
break;
}
}
ob_clean(); //清理缓冲
if($enablegd)
{
//create captcha
$consts = 'cdfgkmnpqrstwxyz23456';
$vowels = 'aek23456789';
for ($x = 0; $x < 6; $x++)
{
$const[$x] = substr($consts, mt_rand(0,strlen($consts)-1),1); //获取$consts中的一个随机数
$vow[$x] = substr($vowels, mt_rand(0,strlen($vowels)-1),1); //获取$vowels中的一个随机数
}
$radomstring = $const[0] . $vow[0] .$const[2] . $const[1] . $vow[1] . $const[3] . $vow[3] . $const[4];
$_SESSION['checkcode'] = $string = substr($radomstring,0,4); //显示4个字符
$imageX = strlen($radomstring)*8; //图像的宽
$imageY = 20; //图像的高
$im = imagecreatetruecolor($imageX,$imageY); //新建一个真彩色图像
//creates two variables to store color
$background = imagecolorallocate($im, rand(180, 250), rand(180, 250), rand(180, 250)); //背景色
$foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)),
imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)),
imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(0, 10)),
imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255))
);
$foreground2 = imagecolorallocatealpha($im, rand(20, 100), rand(20, 100), rand(20, 100),80); //分配颜色并说明透明度
$middleground = imagecolorallocate($im, rand(200, 160), rand(200, 160), rand(200, 160)); //中间背景
$middleground2 = imagecolorallocatealpha($im, rand(180, 140), rand(180, 140), rand(180, 140),80); //中间背景2
//与左上角的颜色相同的都会被填充
imagefill($im, 0, 0, imagecolorallocate($im, 250, 253, 254));
//往图像上写入文字
imagettftext($im, 12, rand(30, -30), 5, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ALGER.TTF', $string[0]);
imagettftext($im, 12, rand(50, -50), 20, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ARIALNI.TTF', $string[1]);
imagettftext($im, 12, rand(50, -50), 35, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ALGER.TTF', $string[2]);
imagettftext($im, 12, rand(30, -30), 50, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/arial.ttf', $string[3]);
//画边框
$border = imagecolorallocate($im, 133, 153, 193);
imagerectangle($im, 0, 0, $imageX - 1, $imageY - 1, $border);
//画一些随机出现的点
$pointcol = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
for ($i=0;$i<80;$i++)
{
imagesetpixel($im,rand(2,$imageX-2),rand(2,$imageX-2),$pointcol);
}
//画随机出现的线
for ($x=0; $x<9;$x++)
{
if(mt_rand(0,$x)%2==0)
{
imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999)); //画线
imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground2); //画椭圆
}
else
{
imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999));
imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);
}
}
//output to browser
header("content-type:image/png\r\n");
imagepng($im);
imagedestroy($im);
}
else
{
$files = glob(XINCHENG_ROOT.'images/checkcode/*.jpg');
if(!is_array($files)) die('请检查文件目录完整性:/images/checkcode/');
$checkcodefile = $files[rand(0, count($files)-1)]; //随机其中一个文件
$_SESSION['checkcode'] = substr(basename($checkcodefile), 0, 4); //获得文件名
header("content-type:image/jpeg\r\n");
include $checkcodefile;
}
?>