下面分享一个php制作的图片验证码案例:案例如下图:
运用PHP GD详细请看:http://www.php.net/manual/zh/book.image.php
后台图片代码如下:
立即学习“PHP免费学习笔记(深入)”;
<?phpsession_start();$image = imagecreatetruecolor(100,30);//创建一个宽100,高度30的图片$bgcolor=imagecolorallocate($image,255,255,255);//图片背景是白色imagefill($image,0,0,$bgcolor);//图片填充白色//随机数/**for($i=0;$i<4;$i++){ $fontsize=6; $fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120)); $fontcontent=rand(0,9); $x=($i*100/4)+ rand(5,10); $y=rand(5,10); imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);}**///随机数据$captch_code='';for($i=0;$i<4;$i++){ $fontsize=6; $fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120)); $data='asdfdfglfg74erf21854hgfhgfhkg4ljkghjtrtywiqpoqpwepdfgvnjytyut12313345645667686797800'; $fontcontent=substr($data,rand(0,strlen($data)),1); $captch_code.=$fontcontent; $x=($i*100/4)+ rand(5,10); $y=rand(5,10); imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);}$_SESSION['authcode']=$captch_code;//随机点for($i=0;$i<200;$i++){ $pointcolor=imagecolorallocate($image,rand(50,120),rand(50,120),rand(50,120)); imagesetpixel($image,rand(1,99),rand(1,99),$pointcolor);}//随机线for($i=0;$i<3;$i++){ $linecolor=imagecolorallocate($image,rand(80,220),rand(80,220),rand(80,220)); imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor);}header("content-type:image/png");imagepng($image);imagedestory($image);?>前台代码如下:
<?php if(isset($_REQUEST['autocode'])){ session_start(); if(strtolower($_POST['autocode']) == $_SESSION['authcode']){ echo '正确'; }else{ echo'错误'; } exit(); }?><!DOCTYPE html><html> <head> <meta chartset="utf-8"> </head> <body> <form method="post" action="form.php"> <p>验证码图片:@@##@@ width="100" height="30" /></p> <p>输入内容:<input type="text" name="autocode" value="" /></p> <p><input type="submit" value="提交" style="padding:6px 20px;"/></p> </form> </body></html>
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号