现在很多页面在使用表单提交时,都会使用到验证码的使用、如何制做一个验证码呢?这里有一个用PHP的方法 以及代码
1、首先在php.ini 配置文件里面把GD库打开 // 在php.ini里面找到 extension=php_gd2.dll 把前面的分号删去。
2、代码:<?php
session_start();
//用php的header方法表名输出内容的格式为png
// 输出图片前,必须提前输出header信息
//imagecreatetruecolor默认输出是黑色的背景
header('content-type:image/png');
//1.通过imagecreatetruecolor 创建一个验证码所需要的底图
$image = imagecreatetruecolor(100,30);
//2.用imagecolorallocate 填充背景
$bgcolor = imagecolorallocate($image,000,255,255);
//3、填充到我们的底图当中
imagefill($image,0,0,$bgcolor);
//定义一个空字符串 来用存储验证码
$captchCode = '';
//for循环来输出验证码位数
for ($i=0; $i
//字体大小为6
$fontSize = 6;
//设置背景字体
$fontColor = imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));
$data = 'abcdefghijklmnopqrstuvwxyz0123456789';
//随机从data 中取出一个字符
$fontContent = substr($data,rand(0,strlen($data)),1);
//将取出的一位字符存储到captchCode当中;
$captchCode .= $fontContent;
$x = ($i * 100/4)+rand(5,10);
$y = rand(5,10);
imagestring($image,$fontSize,$x,$y,$fontContent,$fontColor);
}
//将生成好的验证码保存进session
$_SESSION['code'] = $captchCode;
//循环出200个点
for($i=0;$i<200;$i++){
//创建颜色
$pointColor = imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));
imagesetpixel($image,rand(1,99),rand(1,29),$pointColor);
}
//创建8条横线
for($i=0;$i<3;$i++){
$linecolor = imagecolorallocate($image,rand(60,220),rand(60,220),rand(60,220));
imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor);
}
//返回图片
imagepng($image);
//销毁
imagedestroy($image);
?>
3、在表单input 添加个点击事件 οnclick="this.src = this.src+'?'+ Math.random(0,999) 即点击后刷新验证码