Home  >  Article  >  Backend Development  >  验证码问题求解决

验证码问题求解决

WBOY
WBOYOriginal
2016-06-23 13:49:10743browse

问题:怎样能将图片中的验证码储存起来用于后期的验证?

verficode.php 页面
ob_clean();
for($i=0;$i $num.=dechex(rand(0,15));
}
$num=substr($num,-4,4);
$nowimage=imagecreate(100,30);
imagecolorallocate($nowimage,240,240,240);
for($i=0;$i $font=mt_rand(3,5);
$x=mt_rand(1,8)+100*$i/4;
$y=mt_rand(1,50/4);
$color=imagecolorallocate($nowimage,rand(0,150),rand(0,150),rand(0,150));
imagestring($nowimage,$font,$x,$y,$num[$i],$color);
}
for($i=0;$i $randcolor=imagecolorallocate($nowimage,rand(200,255),rand(200,255),rand(200,255));
imagesetpixel($nowimage,rand()%70,rand()%20,$randcolor);
}
header("content-type:image/png");
imagepng($nowimage);
imagedestroy($nowimage);
?>

index.php 页面


明日商城

<script></script>



require 'conn/head.php';
?>




用户登录




验证码问题求解决
换一张














require 'conn/foot.php';
?>



回复讨论(解决方案)

保存到SESSION

session   可以

一般验证码都是保存到session里面然后采用输入框里面的数据与session里面对应验证码的字段进行匹配后进行判断的。不知道lz是想要什么样的。

把你的验证码方法,封装为类方法,需要的时候直接实例化类就可以用,记得session_start();

<?phpsession_start(); // ??sessionob_clean();for($i=0;$i<4;$i++){$num.=dechex(rand(0,15));	}$num=substr($num,-4,4);$_SESSION['captcha'] = $num; // 把????入session$nowimage=imagecreate(100,30);imagecolorallocate($nowimage,240,240,240);for($i=0;$i<strlen($num);$i++){$font=mt_rand(3,5);$x=mt_rand(1,8)+100*$i/4;$y=mt_rand(1,50/4);$color=imagecolorallocate($nowimage,rand(0,150),rand(0,150),rand(0,150));imagestring($nowimage,$font,$x,$y,$num[$i],$color);}for($i=0;$i<200;$i++){$randcolor=imagecolorallocate($nowimage,rand(200,255),rand(200,255),rand(200,255));imagesetpixel($nowimage,rand()%70,rand()%20,$randcolor);}header("content-type:image/png");imagepng($nowimage);imagedestroy($nowimage);?>


加了?句
session_start();
$_SESSION['captcha'] = $num;

???面
session_start();
if($captcha == $_SESSION['captcha']){  // $captcha?用?提交的???
    // pass
}else{
    // not match
}

#5
但是在index.php 页面接受的$_SESSION['captcha']的数据和验证码显示出来的数据不同步(慢了一步)

index.php 的接受验证码比verficode.php 页面接收的验证码慢了一步

什?叫慢一步?

index.php 先于 verficode.php 执行,慢一步是正常的
所以 zf 采用了保存验证码图片的方案
前几天才在这里讨论过,你可找一下

知道了,谢啦

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
Previous article:IOS 手机验证码的问题Next article:PDO查询不成功!