1. 怎么让php生成的验证码随机数不重复
如果要生成四位数字的验证码,则可以用函数: $srand = rand(1000,9999); 会生成在1000到9999之间的随机数字,如果要生成更多位数的数字,可以更改最孝最大值。
2. 如何用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);//释放图片所占内存
}
3. 验证码怎么用php实现
<?php
/*
* Filename: authpage.php
*/
srand((double)microtime()*1000000);
//验证用户输入是否和验证码一致
if(isset($HTTP_POST_VARS['authinput']))
{
if(strcmp($HTTP_POST_VARS['authnum'],$HTTP_POST_VARS['authinput'])==0)
echo "验证成功!";
else
echo "验证失败!";
}
//生成新的四位整数验证码
while(($authnum=rand()%10000)<1000);
?>
<form action=authpage.php method=post>
<table>
请输入验证码:<input type=text name=authinput style="width:
80px"><br>
<input type=submit name="验证" value="提交验证码">
<input type=hidden name=authnum value=<? echo $authnum; ?>>
<img src=authimg.php?authnum=<? echo $authnum; ?>>
</table>
</form>
代码二:
<?php
/*
* Filename: authimg.php
* Author: hutuworm
* Date: 2003-04-28
* @Copyleft hutuworm.org
*/
//生成验证码图片
Header("Content-type: image/PNG");
srand((double)microtime()*1000000);
$im = imagecreate(58,28);
$black = ImageColorAllocate($im, 0,0,0);
$white = ImageColorAllocate($im, 255,255,255);
$gray = ImageColorAllocate($im, 200,200,200);
imagefill($im,68,30,$gray);
//将四位整数验证码绘入图片
imagestring($im, 5, 10, 8, $HTTP_GET_VARS['authnum'], $black);
for($i=0;$i<50;$i++) //加入干扰象素
{
imagesetpixel($im, rand()%70 , rand()%30 , $black);
}
ImagePNG($im);
ImageDestroy($im);
?>
4. 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的作用,是用来在会话期间内在请求之间传递数据.
5. php验证码判断
session_start();
$str_number = trim($_POST['number']);
if(strtolower($_SESSION['rand'])==strtolower($str_number )){
echo "验证码正确";
}else{
echo "验证码不正确";
}
最好加strtolower函数转换下大小写,这样子,用户在输入时就不用区分大小写了。不然用户体验会很麻烦,这是我个人理解。
6. PHP如何生成加减算法方式的验证码
<?php
namespace mobile\components;
/**
* @author fenghuo
*
* 改造的加减法验证类
* 使用示例 VerifyCode::get(1,2);
* 验证示例 VerifyCode::check($code);
*/
class VerifyCode
{
/**
* php验证码
*/
public static function get($one,$two,$prefix = '', $font_size = 28)
{
//文件头...
ob_get_clean();
header("Content-type: image/png;charset=utf-8;");
//创建真彩色白纸
$width = $font_size*5;
$height = $font_size+1;
$im = @imagecreatetruecolor($width, $height) or die("建立图像失败");
//获取背景颜色
$background_color = imagecolorallocate($im, 255, 255, 255);
//填充背景颜色
imagefill($im, 0, 0, $background_color);
//获取边框颜色
$border_color = imagecolorallocate($im, 200, 200, 200);
//画矩形,边框颜色200,200,200
imagerectangle($im,0,0,$width - 1, $height - 1,$border_color);
//逐行炫耀背景,全屏用1或0
for($i = 2;$i < $height - 2;$i++) {
//获取随机淡色
$line_color = imagecolorallocate($im, rand(200,255), rand(200,255), rand(200,255));
//画线
imageline($im, 2, $i, $width - 1, $i, $line_color);
}
//设置印上去的文字
$firstNum = $one;
$secondNum = $two;
$actionStr = $firstNum > $secondNum ? '-' : '+';
//获取第1个随机文字
$imstr[0]["s"] = $firstNum;
$imstr[0]["x"] = rand(2, 5);
$imstr[0]["y"] = rand(1, 4);
//获取第2个随机文字
$imstr[1]["s"] = $actionStr;
$imstr[1]["x"] = $imstr[0]["x"] + $font_size - 1 + rand(0, 1);
$imstr[1]["y"] = rand(1,5);
//获取第3个随机文字
$imstr[2]["s"] = $secondNum;
$imstr[2]["x"] = $imstr[1]["x"] + $font_size - 1 + rand(0, 1);
$imstr[2]["y"] = rand(1, 5);
//获取第3个随机文字
$imstr[3]["s"] = '=';
$imstr[3]["x"] = $imstr[2]["x"] + $font_size - 1 + rand(0, 1);
$imstr[3]["y"] = 3;
//获取第3个随机文字
$imstr[4]["s"] = '?';
$imstr[4]["x"] = $imstr[3]["x"] + $font_size - 1 + rand(0, 1);
$imstr[4]["y"] = 3;
//文字
$text = '';
//写入随机字串
for($i = 0; $i < 5; $i++) {
//获取随机较深颜色
$text_color = imagecolorallocate($im, rand(50, 180), rand(50, 180), rand(50, 180));
$text .= $imstr[$i]["s"];
//画文字
imagechar($im, $font_size, $imstr[$i]["x"], $imstr[$i]["y"], $imstr[$i]["s"], $text_color);
}
session_start();
$_SESSION[$prefix.'verifycode'] = $firstNum > $secondNum ? ($firstNum - $secondNum) : ($firstNum + $secondNum);
//显示图片
ImagePng($im);
//销毁图片
ImageDestroy($im);
}
public static function check($code)
{
if(trim($_SESSION[$prefix.'verifycode']) == trim($code)) {
return true;
} else {
return false;
}
}
7. php问题,关于生成验证码的问题, 在下面代码的第8行,为什么要 用dechex函数把它转换成十进制
说反了,是转换成16进制,如果不转换16进制那用你这个方法产生的验证码都是一些数字的组合,这里估计目的就是为了得到由(0-9,A-F)这几个字符组成的验证码吧。
8. php 或者 js用户输入完验证码怎么验证
先将随机的验证码数字保存到服务端的session空间里,用户提交后先获取session空间里的数字再跟表单的数字对比就可以了。
9. php中如何使用随机函数rand()生成一个数字验证码
如果要生成四位数字的验证码,则可以用函数:
$srand = rand(1000,9999);
会生成在1000到9999之间的随机数字,如果要生成更多位数的数字,可以更改最小、最大值。