search
Homephp教程php手册php验证码生成代码,php验证码生成

php验证码生成代码,php验证码生成

验证码通常是用来安全保证我们网站注册或登录不被注入的,但为了更安全我们通常会生成一些混合验证码了,下面一起来看看例子.

在我们开发登录模块或者是论坛的灌水模块的时候,为了防止恶意提交,需要用到验证码,验证码就是用来区分人和机器的一种手段,当然这种手段不是万无一失,但总归会起到一些作用.

验证码的实现需要GD库的支持,没有开启GD库的童鞋需开启GD库,其实验证码的制作和使用非常的简单,仅仅只是需要4个步骤就可以搞定:创建验证码底图,显示验证码内容,增加干扰元素,输出验证码,下面我们来进行步骤拆分:

第一步:创建验证码底图

$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)); 
  $data = 'qwertyuipkjhgfdsazxcvbnm23456789'; 
  $content = substr($data, rand(0, strlen($data)), 1); 
  $x = ($i*100/4) + rand(5,9);  
  $y = rand(5,10); 
  imagestring($image, $fontsize, $x, $y, $content, $fontcolor); //在图像上水平输出一行字符串 
} 

第三步:增加干扰元素

// 增加干扰点元素 
for ($i=0; $i < 300; $i++) { 
  $pointcolor = imagecolorallocate($image, rand(50,200), rand(50,200), rand(50,200)); 
  imagesetpixel($image, rand(0,99), rand(0,29), $pointcolor); 
} //开源软件:phpfensi.com 
// 增加干扰线元素  线 和 点 的颜色一定要控制好 要比验证码数字的颜色浅 避免出现验证码数字看不见的现象 
for ($i=0; $i < 4; $i++) { 
  $linecolor = imagecolorallocate($image, rand(100,240), rand(100,240), rand(100,240)); 
  imageline($image, rand(0,99), rand(0,29), rand(0,99), rand(0,29), $linecolor); 
} 

第四步:输出验证码

// 输出创建的图像  在输出图像之前 必须输出头信息 用来规定输出的图像类型 
header("Content-Type: image/png"); 
imagepng($image); 
// 销毁图像 
imagedestroy($image); 

至此,一个简单的验证码就实现了,关于实现验证码的注意事项已经写在了注释里,使用验证码的时候,我们一般都需要用session来保存以便验证,在这里就不作详细介绍。

以上就是php验证码生成代码,在重要的代码里已经进行了注释,大家可以仔细研究注释内容,也很重要,希望大家能够有所收获吧

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools