php generates image verification code
Verification code is very important in WEB applications. It is usually used to prevent users from maliciously submitting forms, such as malicious registration and login, malicious forum flooding, etc. This article will explain how to use PHP to generate common verification codes through examples
Let’s take a look at the rough effect first

Then let’s just post the code next
?
|
$image = imagecreatetruecolor(100, 30); //Create canvas $imagecolor = imagecolorallocate($image, 255, 255, 255); //Background color imagefill($image, 0, 0, $imagecolor); //Fill the background color for($i=0;$i $fontsize = 6; $fontcolor = imagecolorallocate($image, rand(0, 200), rand(0, 200), rand(0, 200)); $fontcontent = rand(0, 9); $x = $i*100/4 rand(5, 15); $y = rand(5, 10); imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor); } for($i=0;$i $pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200)); $x = rand(1, 99); $y = rand(1, 29); imagesetpixel($image, $x, $y, $pointcolor); } for($i=0;$i $linecolor = imagecolorallocate($image, rand(100, 250), rand(100, 250), rand(100, 250)); $x1 = rand(1, 25); $x2 = rand(50, 75); $y1 = rand(1, 15); $y2 = rand(15, 25); imageline($image, $x1, $y1, $x2, $y2, $linecolor); } header("content-type:image/png"); imagepng($image); imagedestroy($image); ?> |
Let me share with you another method that can generate Chinese verification code
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
//1.qi Enable the gd library. The GD library provides a series of APIs for processing images. You can use the GD library to process images or generate images. // On websites, the GD library is usually used to generate thumbnails or to add watermarks to images or to generate reports on website data. session_start();
// Convert the GBK encoded string into a UTF-8 string. The reason why the first parameter is written GBK is because the encoding of this php file stored in the host is GBK encoding // UTF-8 encoding is generally supported by browsers and has strong versatility. Let’s convert it to UTF-8 here $str = iconv("GBK", "utf-8", "All living things, green waters, green mountains, scenic spots and historic sites, open your mind and you will see clouds and clouds, and happiness will always be with you"); if(!is_string($str) || !mb_check_encoding($str,"utf-8")) { exit("Not a string or not utf-8"); } $zhongwenku_size; // Get the length of the string in UTF-8 encoding $zhongwenku_size = mb_strlen($str,"UTF-8");
// Import the above characters into the array $zhongwenku = array(); for( $i=0; $i { $zhongwenku[$i] = mb_substr($str, $i,1,"UTF-8"); }
$result = "";
//Four characters to be written on the picture for($i=0; $i { switch (rand(0, 1)) { case 0: $result.=$zhongwenku[rand(0, $zhongwenku_size-1)]; break; case 1: $result.=dechex(rand(0,15)); break; }
}
$_SESSION["check"] = $result;
// Create a true color picture with a width of 100 and a height of 30 $img = imagecreatetruecolor(100, 30);
//Assign background color $bg = imagecolorallocate($img, 0, 0, 0);
//Assign text color $te = imagecolorallocate($img, 255,255,255);
//Write string on the image //imagestring($img, rand(3,8), rand(1,70), rand(1,10), $result, $te);
//You can write special fonts on the picture according to the loaded font imagettftext($img, 13, rand(2, 9), 20 ,20, $te, "MSYH.TTF",$result);
$_SESSION["check"] = $result;
for($i=0; $i { // $t = imagecolorallocate($img, rand(0, 255),rand(0, 255),rand(0, 255)); // Draw lines imageline($img, 0, rand(0, 20), rand(70,100), rand(0, 20), $te); }
$t = imagecolorallocate($img, rand(0, 255),rand(0, 255),rand(0, 255)); // Add noise to the image for($i=0; $i { imagesetpixel($img, rand(1, 100), rand(1, 30), $t); } //Send http header information. Specify that the jpeg in the image is sent this time header("Content-type: image/jpeg"); // Output jpeg images to the browser imagejpeg($img);
?> |
Let’s give another example
?
2 3
|
session_start(); function random($len) { $srcstr = "1a2s3d4f5g6hj8k9qwertyupzxcvbnm"; mt_srand(); $strs = ""; for ($i = 0; $i $strs .= $srcstr[mt_rand(0, 30)]; } return $strs; } //Randomly generated string $str = random(4); //The width of the verification code image $width = 50; //The height of the verification code image $height = 25; //Declare the image format of the layer to be created @ header("Content-Type:image/png"); //Create a layer $im = imagecreate($width, $height); //Background color $back = imagecolorallocate($im, 0xFF, 0xFF, 0xFF); //Blurred point color $pix = imagecolorallocate($im, 187, 230, 247); //Font color $font = imagecolorallocate($im, 41, 163, 238); //Draw the blur effect point mt_srand(); for ($i = 0; $i imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pix); } //Output characters imagestring($im, 5, 7, 5, $str, $font); //Output rectangle imagerectangle($im, 0, 0, $width -1, $height -1, $font); //Output picture imagepng($im); imagedestroy($im); $str = md5($str); //Select cookies //SetCookie("verification", $str, time() 7200, "/"); //Select Session $_SESSION["verification"] = $str; ?> |
1 |
![]() |
If you want to achieve the "Can't see clearly? Change another" effect, add the following JS to the page
?
2 3
|

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

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

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

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

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

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

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


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

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

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.

Dreamweaver CS6
Visual web development 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
