本文记录从php 下载配置GD图片生成库 到使用该库生成验证码图片,网页上实现验证码。
使用技术:
php使用GD库绘图 【php版本7.3】
php session 缓存
实现最终效果:
一. 配置GD库
1、将php下载包解压后的主目录下的php.ini-development 文件重命名为php.ini。
2、将“;extension_dir = "ext" ”这一选项修改为“extension_dir = "你的php下载包解压后的主目录/ext“”。注意,这个修改的路径是你自己的ext文件夹的完整绝对路径,另外还要把最前面的逗号删除。PS:可以利用很多文本编辑器的搜索功能查找。
3、打开apache压缩包主目录下conf文件夹里的httpd.conf文件,然后搜索 PHPIniDir 这个选项,然后修改它的值为自己下载的php压缩包解压后的主目录的完整绝对路径。
4、自己写一个php文件测试,内容主要是phpinfo(); 其实就是调用这个函数,在浏览器中访问这个文件,去查看它的loadfile选项里是否是正确的自己php压缩包解压后的位置。
5、剩下的开启GD库支持也很简单了。直接搜索 ; extension=php_gd2.dl 然后删除最前面的分号就可以了。
配置方法转载原文地址:https://blog.csdn.net/xieqiaoxiyang/article/details/46554211
二、 写php代码提供完成验证码
本次使用两个php文件:分别是图片生成php文件(getAuthentication.php)和前台页面(auth.php)// 访问auth.php文件效果图:
getAuthentication.php文件代码:
<?php
session_start(); //启动session // 使用session需要调用这个函数,它也是一个扩展
$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['authenticate']=$captch_code;// 设置session
//随机点,生成干扰点
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);
?>
auth.php 的代码:
<?php
//验证表单:
if ($_SERVER['REQUEST_METHOD']=='POST') {
session_start(); //session也是一个扩展,使用这个扩展必须先调用这个函数。
echo $_SESSION['authenticate'];
if(!empty($_POST['authenticate'])&&!empty($_SESSION['authenticate'])) {
if($_POST['authenticate']==$_SESSION['authenticate']) {
$_GLOBALS['message'] = '正确';
}
else {
$_GLOBALS['message'] = '错误';
}
}
else {
$_GLOBALS['message'] = '没有输入验证码';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<h2><?php echo $_GLOBALS['message'] ; ?></h2>
<meta charset="UTF-8">
<title>验证</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="text" name="authenticate"><img src="getAuthentication.php" id="img">
<button type="submit">提交</button>
</form>
</body>
<script type="text/javascript">
// 点击验证码图片,进行更换验证码
document.getElementById("img").onclick = function() {
this.src='getAuthentication.php?r='+Math.random(); ///这样就不需要AJAX请求了,比较方便
}
</script>
</html>