captcha.php(PHP产生验证码并储存Session):

<?php

//开启Session
session_start(); //绘制底图
$image = imagecreatetruecolor(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 = '23456789ABCDEFGHJKLMNOPQRTUVWXYZabcdefghjkmnopqrtuvwxy';//随机字符串的字典
$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
$_SESSION['authcode'] = $captch_code; //点干扰
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, 99), $pointcolor);
} //线干扰
for($i = 0; $i < 6; $i++){
$linecolor = imagecolorallocate($image, rand(80, 220), rand(80, 220), rand(80, 220));
imageline($image, rand(1, 99), rand(1, 99), rand(1, 99), rand(1, 99), $linecolor);
} //输出图片内容
header('content-type:image/png');//输出内容的格式
imagepng($image);//输出内容
imagedestroy($image);//销毁资源 ?>

  

  

captcha-form.html(Web表单验证):

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>验证码</title>
</head> <body>
<form method="post" action="./captcha-result.php"> <p>
验证码图片:
<img id="captcha-img" border="1" src="./captcha.php?r=0">
<a href="javascript:void(0);" onclick="getCaptchaImg();">看不清</a>
</p> <p>请输入图片中的内容:<input id="authcode" type="text" name="authcode" value=""></p> <p><input type="submit" value="提交" style="padding:6px 20px;"></p> </form> <script>
//动态获取验证码
function getCaptchaImg(){
//从服务器获取新的验证码
document.getElementById('captcha-img').src='./captcha.php?r='+Math.random();
//清空文本框里已输入的内容
document.getElementById('authcode').value="";
}
</script> </body>
</html>

  

生成的验证码:  

captcha-result.php(PHP判断验证码是否正确):

<?php

//验证验证码是否正确
if(isset($_REQUEST['authcode'])){
//开启Session
session_start(); //strtolower()将字符串都转换成小写,将验证码设置为不区分大小写型
if(strtolower($_REQUEST['authcode']) == strtolower($_SESSION['authcode'])){
echo "输入正确";
}else{
echo "输入错误";
}
exit();
} ?>
  

  

所用到的函数原型:

<?php

//imagecreatetruecolor() 返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像。
resource imagecreatetruecolor ( int $width , int $height ); //imagecolorallocate() 返回一个标识符,代表了由给定的 RGB 成分组成的颜色。red,green 和 blue 分别是所需要的颜色的红,绿,蓝成分。
int imagecolorallocate ( resource $image , int $red , int $green , int $blue ); //imagefill() 在 image 图像的坐标 x,y(图像左上角为 0, 0)处用 color 颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)。
bool imagefill ( resource $image , int $x , int $y , int $color ); //imagestring() 用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 坐标处(这是字符串左上角坐标,整幅图像的左上角为 0,0)。如果 font 是 1,2,3,4 或 5,则使用内置字体。
bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col ); //imagesetpixel() 在 image 图像中用 color 颜色在 x,y 坐标(图像左上角为 0,0)上画一个点。
bool imagesetpixel ( resource $image , int $x , int $y , int $color ); //imageline() 用 color 颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段。
bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color ); //imagepng() 将 GD 图像流(image)以 PNG 格式输出到标准输出(通常为浏览器),或者如果用 filename 给出了文件名则将其输出到该文件。
bool imagepng ( resource $image [, string $filename ] ); //imagedestroy() 释放与 image 关联的内存。image 是由图像创建函数返回的图像标识符,例如 imagecreatetruecolor()。
bool imagedestroy ( resource $image ); ?>

  

  

本文链接:https://www.cnblogs.com/connect/p/php-captcha-image.html

PHP实现验证码制作的更多相关文章

  1. webform 图片验证码制作

    界面:1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.as ...

  2. MVC-简单验证码制作

    1.制作验证码: using System; using System.Collections.Generic; using System.Drawing; using System.Drawing. ...

  3. webform:图片水印、验证码制作

    一.图片水印 1:引命名空间System.Drawing; 前端代码 <div> <asp:FileUpload ID="FileUpload1" runat=& ...

  4. Java实现验证码制作之一Kaptcha验证码

    Kaptcha验证码 是google提供的验证码插件,使用起来相对简单,设置的干扰线以及字体扭曲不易让其他人读取破解. 这里我们需要 导入一个 kaptcha-2.3.jar  下载地址:http:/ ...

  5. Java实现验证码制作之一自己动手

    以前弄验证码都是现找现用,下面是自己跟着敲代码弄好的,记录一下,分享给大家. 我这里用的是Servlet ,Servlet代码如下 import java.awt.Color;import java. ...

  6. php验证码制作

    目标: 使用php生成验证码 成品: 逻辑代码: authcode.php <?php header("Content-type:image/png"); session_s ...

  7. PHP - 验证码制作加验证

    一,主页 index.php   <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  8. PHP绘图案例讲解验证码制作

    <?php header("Content-type: image/png");//声明浏览器解析为图片 $width=200; $height=100; $color1=i ...

  9. Code笔记 之:注册页面验证码

    文章内容包括: 1.验证码制作 -- 中文|字母|数字|…… 2.图文验证码 -- 图片防盗链(PHP而非JS) 3.JS防止右键点击图片 4.input表单输入框不记录输入过的信息 5.CSS+di ...

随机推荐

  1. 系统运维|IIS的日志设置

    摘要: 1.服务器告警,磁盘资源不足 2.检查发现是IIS日志没有清理并且设置有误.在E盘占用了200G的空间 3.原则上IIS日志不能放在C盘,避免C盘写满了导致操作系统异常 4.附上IIS日志按天 ...

  2. 彻底修改 Windows 系统用户名

    在 Windows 安装的时候会输入一个用户名,电脑店装的一般都会给你设置成Admin之类的.这个时候你想要改成自己的,一般都是直接在 控制面板 > 用户帐户和家庭安全 > 用户帐户 &g ...

  3. Linux 小知识翻译 - 「端口限制」

    上次说了端口号相关的内容,这次聊聊「端口限制」的事. 经常看到关于安全的书籍上会说「不要开放多余的端口」,那么,如何限制端口才好呢? 实际,端口限制的方法大体上分的话有2种. 其一,「通过应用程序来处 ...

  4. Activiti工作流搭建---初始化数据库

    Activiti介绍 Activiti5是由Alfresco软件在2010年5月17日发布的业务流程管理(BPM)框架,它是覆盖了业务流程管理.工作流.服务协作等领域的一个开源的.灵活的.易扩展的可执 ...

  5. Handler实现线程间的通信1

    通过Handler实现线程间的通信,在主线程当中实现Handler的handlerMessage()方法,在WorkerThread中通过Handler发送消息 Handler实现线程间的通信实例: ...

  6. Find a way

    Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year ...

  7. eclipse 自动创建web.xml

    eclipse 自动创建web.xml 以前每次创建web项目的时候,总是从其他的项目中拷贝一个web.xml文件到新项目,然后改吧改吧就可以了,但是这终究是一件麻烦的事,要是能够通过软件自动创建,那 ...

  8. MyISAM to InnoDB: Why and How(MYSQL官方译文)

    原文地址:https://www.mysql.com/why-mysql/presentations/myisam-2-innodb-why-and-how/ MySQL使用一个插拔式的存储引擎架构, ...

  9. Scout YYF I POJ - 3744(矩阵优化)

    题意:一条路上有n个地雷,给出地雷的位置.某人从起点(位置1)出发,走一步的概率是p,走两步的概率是(1-p),然后问有多少概率走过这个雷区. 思路: 只要走过最后一个地雷就代表走过雷区了. 而每到 ...

  10. 【转】Emgu 图像阈值

    原文地址:http://www.cnblogs.com/CoverCat/p/5043833.html 转载,备查 Visual Studio Community 2015 工程和代码:http:// ...