Home >Backend Development >PHP Tutorial >验证码有关问题求解决

验证码有关问题求解决

WBOY
WBOYOriginal
2016-06-13 12:06:39915browse

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

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   可以
------解决思路----------------------
<br /><?php<br />session_start(); // 開啓session<br />ob_clean();<br />for($i=0;$i<4;$i++){<br />$num.=dechex(rand(0,15));	<br />}<br />$num=substr($num,-4,4);<br />$_SESSION['captcha'] = $num; // 把驗證碼寫入session<br />$nowimage=imagecreate(100,30);<br />imagecolorallocate($nowimage,240,240,240);<br />for($i=0;$i<strlen($num);$i++){<br />$font=mt_rand(3,5);<br />$x=mt_rand(1,8)+100*$i/4;<br />$y=mt_rand(1,50/4);<br />$color=imagecolorallocate($nowimage,rand(0,150),rand(0,150),rand(0,150));<br />imagestring($nowimage,$font,$x,$y,$num[$i],$color);<br />}<br />for($i=0;$i<200;$i++){<br />$randcolor=imagecolorallocate($nowimage,rand(200,255),rand(200,255),rand(200,255));<br />imagesetpixel($nowimage,rand()%70,rand()%20,$randcolor);<br />}<br />header("content-type:image/png");<br />imagepng($nowimage);<br />imagedestroy($nowimage);<br />?><br />


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

驗證頁面
session_start();
if($captcha == $_SESSION['captcha']){  // $captcha為用戶提交的驗證碼
    // pass
}else{
    // not match
}
------解决思路----------------------
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