您现在的位置是:首页 > IT分类 > PHP网站首页PHPPHP图片验证码
PHP图片验证码
- PHP
- 2020-03-21 12:01:10
简介今天闹了个笑话,我写form标签提交验证码时,input的type属性,用了button,结果我是使劲在那提交,地址栏呐就是没动静,找了半天才找到,哈哈哈....最后发现是这个button没反应,换成了sub
今天闹了个笑话,我写form标签提交验证码时,input的type属性,用了button,结果我是使劲在那提交,地址栏呐就是没动静,找了半天才找到,哈哈哈....最后发现是这个button没反应,换成了submit就行了,从这个细节反应出一个问题,代码不练就会越来越生疏了....
言归正传,先做一个图片,命名:1.php
session_start();
header ('Content-Type: image/png');
$image=imagecreatetruecolor(100, 30);
$color=imagecolorallocate($image, 255, 255, 255);
imagefill($image, 20, 20, $color);
$codeimg='';
for($i=0;$i<4;$i++){
$fontSize=8;
$x=rand(5,10)+$i*100/4;
$y=rand(5, 15);
$data='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$string=substr($data,rand(0, strlen($data)),1);
$codeimg.=$string;
$color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
imagestring($image, $fontSize, $x, $y, $string, $color);
}
$_SESSION['codeimg']=$codeimg;//存储在session里
for($i=0;$i<200;$i++){
$pointColor=imagecolorallocate($image, rand(100, 255), rand(100, 255), rand(100, 255));
imagesetpixel($image, rand(0, 100), rand(0, 30), $pointColor);
}
for($i=0;$i<2;$i++){
$linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
}
imagepng($image);
imagedestroy($image);
HTML部分
<form action='2.php' method='GET'>
<input type="text" name="codeimg" placeholder="验证码">
<img src="1.php" onclick="this.src='1.php?'+Math.random()" alt="看不清楚,点击刷新">
<input type='submit' value="提交" />
</form>
2.php
session_start ();
if(isset($_GET['codeimg'])){
//取值转换成小写
if(strtolower($_SESSION['codeimg'])===strtolower($_GET['codeimg'])){
echo '验证成功<br/>';
}else {
echo '验证错误<br/>';
}
}
转载:
感谢您对蓝天个人博客网站平台的认可,非常欢迎各位朋友分享到个人站长或者朋友圈,但转载请说明文章出处“来源蓝天个人博客 http://www.along168.cn”。
上一篇:PHP常用数组方法:统计
相关文章
文章评论
- PHP
- 2020-03-21 12:01:10
今天闹了个笑话,我写form标签提交验证码时,input的type属性,用了button,结果我是使劲在那提交,地址栏呐就是没动静,找了半天才找到,哈哈哈....最后发现是这个button没反应,换成了submit就行了,从这个细节反应出一个问题,代码不练就会越来越生疏了....
言归正传,先做一个图片,命名:1.php
言归正传,先做一个图片,命名:1.php
session_start();
header ('Content-Type: image/png');
$image=imagecreatetruecolor(100, 30);
$color=imagecolorallocate($image, 255, 255, 255);
imagefill($image, 20, 20, $color);
$codeimg='';
for($i=0;$i<4;$i++){
$fontSize=8;
$x=rand(5,10)+$i*100/4;
$y=rand(5, 15);
$data='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$string=substr($data,rand(0, strlen($data)),1);
$codeimg.=$string;
$color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
imagestring($image, $fontSize, $x, $y, $string, $color);
}
$_SESSION['codeimg']=$codeimg;//存储在session里
for($i=0;$i<200;$i++){
$pointColor=imagecolorallocate($image, rand(100, 255), rand(100, 255), rand(100, 255));
imagesetpixel($image, rand(0, 100), rand(0, 30), $pointColor);
}
for($i=0;$i<2;$i++){
$linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
}
imagepng($image);
imagedestroy($image);
HTML部分
<form action='2.php' method='GET'>
<input type="text" name="codeimg" placeholder="验证码">
<img src="1.php" onclick="this.src='1.php?'+Math.random()" alt="看不清楚,点击刷新">
<input type='submit' value="提交" />
</form>
2.php
session_start ();
if(isset($_GET['codeimg'])){
//取值转换成小写
if(strtolower($_SESSION['codeimg'])===strtolower($_GET['codeimg'])){
echo '验证成功<br/>';
}else {
echo '验证错误<br/>';
}
}
转载: 感谢您对蓝天个人博客网站平台的认可,非常欢迎各位朋友分享到个人站长或者朋友圈,但转载请说明文章出处“来源蓝天个人博客 http://www.along168.cn”。
上一篇:PHP常用数组方法:统计