制作PHP验证码
来源:互联网 发布:全国污染源普查数据库 编辑:程序博客网 时间:2024/05/01 00:27
- 列表内容
现在为了防治恶意注册,机器注册。验证码已经是必不可少的一项了,今天我们看看如何用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);}
//输出图像
ob_clean(); //*****在这里,没有这句会说图片内部有错******//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
xsize,int y_size ) imagecreatetruecolor() 返回一个图像标识符,代表了一幅大小为
x_size 和 y_size 的黑色图像。imagecolorallocate — 为一幅图像分配颜色 int imagecolorallocate ( resource
image,int red , intgreen,int blue ) imagecolorallocate()
返回一个标识符,代表了由给定的 RGB 成分组成的颜色。red,green 和 blue 分别是所需要的颜色的红,绿,蓝成分。这些参数是
0 到 255 的整数或者十六进制的 0x00 到 0xFF。imagecolorallocate()必须被调用以创建每一种用在imagefill — 区域填充 bool imagefill ( resource
image,int x , inty,int color ) imagefill() 在 image图像的坐标 x,y(图像左上角为 0, 0)处用
color颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)。imagerectangle — 画一个矩形 bool imagerectangle ( resource
image,int x1 , inty1,int x2 , inty2,int col ) imagerectangle() 用
col 颜色在 image 图像中画一个矩形,其左上角坐标为 x1, y1,右下角坐标为 x2, y2。图像的左上角坐标为 0, 0。imageline — 画一条线段 bool imageline ( resource
image,int x1 , inty1,int x2 , inty2,int color ) imageline() 用 color颜色在图像
image 中从坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段。imagestring — 水平地画一行字符串 bool imagestring ( resource
image,int font , intx,int y , strings,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哦
<?phpsession_start();echo $_SESSION['code'];?>
- PHP制作验证码
- php 验证码制作
- 制作PHP验证码
- php验证码制作
- php 制作验证码
- php制作验证码
- 验证码制作php
- php制作验证码
- PHP制作验证码
- php验证码制作
- php制作验证码
- php验证码制作
- PHP+验证码制作
- PHP制作验证码
- PHP制作验证码
- php制作简单验证码
- php验证码的制作
- 怎样制作PHP验证码
- 黑马程序员- java中的排序
- 更改make版本由于编译4.0Android需要3.81make所以重新安装
- Dui界面布局无法响应鼠标点击消息响应
- Particle System调整
- 贝叶斯网络
- 制作PHP验证码
- 河南权威的自闭症yiyuan
- 密码学_这里没有key
- Jquery里面三个文档操作方法比较-empty(),detach()和remove
- SQL Server自增长列插入指定值 -- SET IDENTITY_INSERT ON|OFF
- Structual设计--Facade模式
- update Yosemite ,prompt " * you need to install the legacy Java SE 6 Runtime"
- @Repository、@Service、@Controller 和 @Component
- 假如你手上有50万,你会怎么样?