Home >Backend Development >PHP Tutorial >Simple example of PHP verification code implementation code_PHP tutorial

Simple example of PHP verification code implementation code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:11:18912browse

I have talked about many examples of PHP verification codes. Below I will share a good PHP verification code program from a friend. It is quite simple and you can refer to it.

I have been bored recently and have nothing to do, so I started to share some PHP code. The codes are relatively simple, but more practical.

This was written four years ago. A few lines of code implement the four-digit random number PHP verification code function.

The code is as follows
 代码如下 复制代码

session_start();
header("Content-type: image/jpeg");
$img = imagecreate(50,20);
$white = imagecolorallocate($img,211,213,193);
imagefill($img,0,0,$white);
for($i=0;$i<4;$i++){
$r[$i] = rand(0,9);
}
$_SESSION['valid'] = implode("",$r); //在这里写入到session可做后期验证
for($i=0;$i<4;$i++){

if(rand(0,9)%2==0){
imagechar($img,5,($i+1)*8,4,$r[$i],imagecolorallocate($img,rand(0,150),rand(0,150),rand(0,150)));
}else{
imagechar($img,5,($i+1)*8,2,$r[$i],imagecolorallocate($img,rand(0,150),rand(0,150),rand(0,150)));
}

}
imagejpeg($img);

Copy code
session_start();
header("Content-type: image/jpeg");
$img = imagecreate(50,20);
$white = imagecolorallocate($img,211,213,193);
imagefill($img,0,0,$white);
for($i=0;$i<4;$i++){
$r[$i] = rand(0,9);
}
$_SESSION['valid'] = implode("",$r); //Write to session here for later verification
for($i=0;$i<4;$i++){

if(rand(0,9)%2==0){
imagechar($img,5,($i+1)*8,4,$r[$i],imagecolorallocate($img,rand(0,150),rand(0,150),rand(0,150)));
}else{
imagechar($img,5,($i+1)*8,2,$r[$i],imagecolorallocate($img,rand(0,150),rand(0,150),rand(0,150)));
}

http://www.bkjia.com/PHPjc/629610.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/629610.htmlTechArticleI have talked about many examples of PHP verification code. Now I will share a good PHP verification code program from a friend. , it is quite simple, friends can refer to it. I've been bored and had nothing to do lately, so I started doing some 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