怎样用PHP制作验证码呢?
生成验证码无非就那么几个步骤,首先是获取一个随机字符串,然后创建一个布画,将生成的字符串写到布画上,我们还可以在布画上画线画雪花,现在帖一段生成验证码的代码。
源代码:
<?php
session_start(); //开启session
//创建随机码,并保存在session中
for($i=0;$i<4;$i++)
{
$_nmsg.=dechex(mt_rand(0,15));
}
//保存到session中
$_SESSION['code']=$_nmsg;
//设置图片长和高
$_width=75;
$_height=25;
//创建一张图像
$_img=imagecreatetruecolor($_width,$_height);
//白色背景
$_white=imagecolorallocate($_img,255,255,255);
//填充到背景上
imagefill($_img,0,0,$_white);
//黑色边框
$_black=imagecolorallocate($_img,0,0,0);
imagerectangle($_img,0,0,$_width-1,$_height-1,$_black);
//随即画出5个线条
for($i=0;$i<5;$i++)
{
$_rnd_color=imagecolorallocate($_img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageline($_img,mt_rand(0,$_width),mt_rand(0,$_height),mt_rand(0,$_width),mt_rand(0,$_height),$_rnd_color);
}
//雪花
for($i=0;$i<10;$i++)
{
$_rnd_color=imagecolorallocate($_img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($_img,1,mt_rand(1,$_width),mt_rand(1,$_height),"*",$_rnd_color);
}
//输出验证码
for($i=0;$i<strlen($_SESSION['code']);$i++)
{
imagestring($_img,5,10+$i*15,mt_rand(0,10),$_SESSION['code'][$i],$_blackr);
}
//输出图像
header('Content-Type:image/png');
imagepng($_img);
//销毁图像
imagedestroy($_img);
?>
代码中将使用以下函数:
mt_rand ― 生成更好的随机数
int mt_rand ([ int $min ], int $max )很多老的 libc 的随机数发生器具有一些不确定和未知的特性而且很慢。PHP 的 rand() 函数默认使用 libc 随机数发生器。
mt_rand()函数是非正式用来替换它的。该函数用了Mersenne Twister中已知的特性作为随机数发生器,它可以产生随机数值的平均速度比 libc 提供的 rand() 快四倍。
dechex ― 十进制转换为十六进制返回一字符串,包含有给定 number参数的十六进制表示。所能转换的最大数值为十进制的 4294967295,其结果为 "ffffffff"。
imagecreatetruecolor ― 新建一个真彩色图像
resource imagecreatetruecolor ( int $x_size , int $y_size )
imagecreatetruecolor() 返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像。
imagecolorallocate ― 为一幅图像分配颜色
int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
imagecolorallocate() 返回一个标识符,代表了由给定的 RGB 成分组成的颜色。red,green 和 blue 分别是所需要的颜色的红,绿,蓝成分。这些参数是 0 到 255 的整数或者十六进制的 0x00 到 0xFF。imagecolorallocate()必须被调用以创建每一种用在 image 所代表的图像中的颜色。
imagefill ― 区域填充
bool imagefill ( resource $image , int $x , int $y , int $color )
imagefill() 在 image图像的坐标 x,y(图像左上角为 0, 0)处用 color颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)。
imagerectangle ― 画一个矩形
bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col )
imagerectangle() 用 col 颜色在 image 图像中画一个矩形,其左上角坐标为 x1, y1,右下角坐标为 x2, y2。图像的左上角坐标为 0, 0。
imageline ― 画一条线段
bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
imageline() 用 color颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段。
imagestring ― 水平地画一行字符串
bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
imagestring() 用 col颜色将字符串 s 画到 image所代表的图像的 x,y坐标处(这是字符串左上角坐标,整幅图像的左上角为 0,0)。如果 font 是 1,2,3,4 或 5,则使用内置字体。
imagepng ― 以 PNG 格式将图像输出到浏览器或文件
imagepng() 将 GD 图像流(image)以 PNG 格式输出到标准输出(通常为浏览器),或者如果用 filename 给出了文件名则将其输出到该文件。
imagedestroy ― 销毁一图像
imagedestroy() 释放与 image 关联的内存。
将源代码保存为code.php是个php文件,我们该如何使用他呢?
imagepng已经将这个php文件输出成了png文件
直接调用就可以了
<img src="mycode.php"/>
如果要使用验证码,记得开启session哦
<?php
session_start();
echo $_SESSION['code'];
?>
怎样用PHP制作验证码呢?的更多相关文章
- (一)学习MVC之制作验证码
制作验证码的方法在@洞庭夕照 看到的,原文链接:http://www.cnblogs.com/mzwhj/archive/2012/10/22/2720089.html 现自己利用该方法制作一个简单的 ...
- Jsp制作验证码
验证码 验证码(CAPTCHA)是"Completely Automated Public Turing test to tell Computers and Humans Apart&qu ...
- php 制作验证码不显示的问题
php制作验证码的代码,这里就不多说了,网上有很多的,这里说一些可能遇到的问题. 1. 首先是检查自己的php.ini文件,是否支持gd库. 2.保证代码没有出问题. 3.检查字体文件路径是否正确. ...
- java制作验证码(java验证码小程序)
手动制作java的验证码 Web应用验证码的组成: (1)输入框 (2)显示验证码的图片 验证码的制作流程: 生成验证码的容器使用 j2ee的servlet 生成图片需要的类: (1) Buffere ...
- 使用python制作验证码
方法一 简单型:使用random模块制作一个随机字母与数字的验证码 import random def make_code(n): res='' for i in range(n): num=str( ...
- 在ashx文件中制作验证码(使用session要继承IRequiresSessionState)
必须继承System.Web.SessionState.IRequiresSessionState接口,才能实现Session读写! System.Web.SessionState的一些接口 IRea ...
- jcaptcha sample 制作验证码
Skip to end of metadata Created by marc antoine garrigue, last modified by Jeremy Waters on Feb 23, ...
- java制作验证码
建立一个web工程
- DAY19-Pillow制作验证码
PIL:Python Imaging Library,已经是Python平台事实上的图像处理标准库了.PIL功能非常强大,但API却非常简单易用. 由于PIL仅支持到Python 2.7,加上年久失修 ...
随机推荐
- iOS系统版本简介
iOS系统版本简介 ⽬目前iOS设备所⽀支持的最主流操作系统是iOS6,⼤大概占了93%,⽽而使 ⽤用iOS5的iOS设备⼤大概占6%,剩下的只有1%.( 根据苹果的官⽅方数据 ) 从iOS1到现在的 ...
- Android界面实现----PagerTabStrip绚丽的滑动标签
在ViewPager这种可以滑动的控件上,总是有很多的文章可以做.Android自带的控件,实现一个指示器,这个控件,就是support-v4包里面的PagerTabStrip控件. 首先,我们先看一 ...
- [算法]A General Polygon Clipping Library
A General Polygon Clipping Library Version 2.32 http://www.cs.man.ac.uk/~toby/alan/software/gpc.h ...
- ios理解 -- Pro Mutlithreading and Memory Management for iOS and OS X with ARC, Grand Central Dispatch, and Blocks
Capturing automatic variables Next, you need to learn what the “together with automatic (local) vari ...
- js获取页面及个元素高度、宽度
网页可见区域宽: document.body.clientWidth; 网页可见区域高: document.body.clientHeight; 网页可见区域宽: document.body.offs ...
- Xshell下VI打开文件中文乱码解决
修改 /etc/sysconfig/i18n 成如下值:LANG="zh_CN.utf8"LANGUAGE="zh_CN.utf8"SUPPORTED=&quo ...
- 一次数据库hang住的分析过程
现象: 普通用户和sysdba都无法登陆,业务中断 分析过程: 1.先做hanganalyze和systemstate dump $sqlplus -prelim "/as sysdba&q ...
- Java基础之读文件——使用通道读二进制数据(ReadPrimes)
控制台程序,本例读取Java基础之写文件部分(PrimesToFile)写入的primes.bin. import java.nio.file.*; import java.nio.*; import ...
- System.Windows.Forms.Timer与System.Timers.Timer的区别(zz)
.NET Framework里面提供了三种Timer: System.Windows.Forms.Timer System.Timers.Timer System.Threading.Timer VS ...
- 当As3遇见Swift(三)
类 As3 Swift中似乎没有包,包路径的概念.因而显得简洁的多. package { public class ShuaiGe { } } Swift类 class ShuaiGe{ } 类的构造 ...