一个非常简单的PHP验证码生成例子,在这个PHP验证码生成我们需要使用到php_gd2.dll扩展了,下面我就来给大家整个例子了,希望对各位朋友会带来帮助哦。
使用PHP的GD库扩展类制作验证码的基本步骤:
启用php.ini配置文件中的extension=php_gd2.dll扩展(打开php.ini配置文件,去掉行首的分号后重启Apache即可);
下面是PHP生成验证码的代码:
<?php for($i=0;$i<5;$i++){ $suijishu.=dechex(rand(1,15));//使用循环产生一个十六进制格式的五位数 } setcookie("useryzm",$suijishu);//创建一个名为useryzm、值为$suijishu的cookie $img=imagecreatetruecolor(100,30);//创建一个宽100高30的图片验证码 $bg=imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));//定义验证码的背景颜色 $sc=imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));//定义验证码字体的颜色 imagestring($img,5,rand(0,50),rand(0,15),$suijishu,$sc);//定义字体的位置(左上角为坐标原点) for($k=0;$k<10;$k++){ imageline($img,rand(1,100),rand(1,100),rand(1,100),rand(1,100),$sc);//在验证码上绘制直线(扰乱作用) imagesetpixel($img,rand(1,100),rand(1,30),$sc);//在验证码上绘制点(扰乱作用) } ob_clean();//清空输出缓冲区(这句很重要) header("Content-type:image/jpeg");//定义输出图片的格式(image/后面可以是jpeg或png或gif等图片格式) imagejpeg($img);//输出图象 imageDestroy($img);//释放内存,完成创建 ?>
下面是HTML中调用验证码的代码:
<html> ... <script language="javascript"> function changeyzm(){ var img = document.getElementById("yzmimg").src = "gdyzm.php?id="+Math.random()*10; return ; } </script> ... <img src="/static/imghwm/default1.png" data-src="gdyzm.php" class="lazy" name="yzmimg" style="max-width:90%" style="max-width:90%" id="yzmimg" title="点击刷新" onclick="changeyzm();" / alt="PHP验证码生成例子 " > ... </html>
如果出现"图像“XXXX”因其本身有错无法显示"的错误,很可能是漏了ob_clean();这句,或是文件的物理编码出了问题(修改物理编码的方法可以参考PHP创建utf8文件)
如果没有加ob_clean();这句,在本地测试且物理编码为ANSI时没问题,物理编码为UTF-8时则会报错(图像“XXXX”因其本身有错无法显示)
教程链接:
随意转载~但请保留教程地址★

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool