最终效果
操作步骤
在action下新建一个captcha.php文件,内容是:
$bgcolor = imagecolorallocate($image, 255, 255, 255); //填充颜色 imagefill($image, 0, 0, $bgcolor); //设置验证码内容 //定义验证码的内容 $content = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789"; //创建一个变量存储产生的验证码数据,便于用户提交核对 $captcha = ""; for ($i = 0; $i < 4; $i++) { // 字体大小 $fontsize = 10; // 字体颜色 $fontcolor = imagecolorallocate($image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120)); // 设置字体内容 $fontcontent = substr($content, mt_rand(0, strlen($content)), 1); $captcha .= $fontcontent; // 显示的坐标 $x = ($i * 100 / 4) + mt_rand(5, 10); $y = mt_rand(5, 10); // 填充内容到画布中 imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor); } $_SESSION["captcha"] = $captcha; //向浏览器输出图片头信息 header('content-type:image/png'); //输出图片到浏览器 imagepng($image); //销毁图片 imagedestroy($image); ?>
signpop.js里面加入
<h6>\ <label for="inputEmail2"">验证码</label>\ <div>\ <input style="width: 190px;display: inline;" type="text" name="captcha" class="form-control" id="captcha" placeholder="验证码">\ <img src="1.php" onclick="this.src=\'1.php?\'+new Date().getTime();" width="100" height="30">\ </div>\ </h6>\
最后在action/log.php里面加入
session_start(); $captcha = ( isset($_POST['captcha']) ) ? trim($_POST['captcha']) : null; if(strtolower($_SESSION["captcha"]) == strtolower($captcha)){ $_SESSION["captcha"] = ""; }else{ print_r(json_encode(array('error'=>1, 'msg'=>'验证码错误'))); exit(); }