Home > Backend Development > PHP Tutorial > PHP uses verification code to prevent malicious registration of study notes_PHP tutorial

PHP uses verification code to prevent malicious registration of study notes_PHP tutorial

WBOY
Release: 2016-07-13 10:50:31
Original
1068 people have browsed it

A commonly used method to prevent malicious registration is to use verification codes. When users submit registration information, I randomly generate a graphic verification code, so that only humans can recognize it. Of course, simple verification code machines can recognize it, so It's better to be more complicated.


Today we will study the PHP verification code. We implement it through a simple digital verification code. First, let’s write a code to generate the verification code:

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

//随机生成一个4位数的数字验证码

$num=”"; for($i=0;$i<4;$i++){ $num .= rand(0,9); }

//4位验证码也可以用rand(1000,9999)直接生成

//将生成的验证码写入session,备验证页面使用

Session_start(); $_SESSION["Checknum"] = $num;

//创建图片,定义颜色值 Header(“Content-type: image/PNG”);

srand((double)microtime()*1000000);

$im = imagecreate(60,20);

$black = ImageColorAllocate($im, 0,0,0);

$gray = ImageColorAllocate($im, 200,200,200);

imagefill($im,0,0,$gray);

//随机绘制两条虚线,起干扰作用

$style = array($black, $black, $black, $black, $black, $gray, $gray, $gray, $gray, $gray);

imagesetstyle($im, $style);

$y1=rand(0,20); $y2=rand(0,20); $y3=rand(0,20); $y4=rand(0,20);

imageline($im, 0, $y1, 60, $y3, IMG_COLOR_STYLED);

imageline($im, 0, $y2, 60, $y4, IMG_COLOR_STYLED)

//在画布上随机生成大量黑点,起干扰作用;

for($i=0;$i<80;$i++) {

imagesetpixel($im, rand(0,60), rand(0,20), $black); }

//将四个数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成

$strx=rand(3,8);

for($i=0;$i<4;$i++){

$strpos=rand(1,6); imagestring($im,5,$strx,$strpos, substr($num,$i,1), $black); $strx+=rand(8,12);

}

ImagePNG($im); ImageDestroy($im);

?>

//Randomly generate a 4-digit verification code

   

代码如下 复制代码

验证码 :
验证码 :
$num=""; for($i=0;$i<4;$i++){ $num .= rand(0,9); }<🎜> <🎜>//The 4-digit verification code can also be generated directly using rand(1000,9999)<🎜> <🎜>//Write the generated verification code into the session for use on the verification page<🎜> <🎜>Session_start(); $_SESSION["Checknum"] = $num;<🎜> <🎜>//Create an image and define the color value Header(“Content-type: image/PNG”);<🎜> <🎜>srand((double)microtime()*1000000);<🎜> <🎜>$im = imagecreate(60,20);<🎜> <🎜>$black = ImageColorAllocate($im, 0,0,0);<🎜> <🎜>$gray = ImageColorAllocate($im, 200,200,200);<🎜> <🎜>imagefill($im,0,0,$gray);<🎜> <🎜>//Draw two dotted lines randomly to act as interference<🎜> <🎜>$style = array($black, $black, $black, $black, $black, $gray, $gray, $gray, $gray, $gray);<🎜> <🎜>imagesetstyle($im, $style);<🎜> <🎜>$y1=rand(0,20); $y2=rand(0,20); $y3=rand(0,20); $y4=rand(0,20);<🎜> <🎜>imageline($im, 0, $y1, 60, $y3, IMG_COLOR_STYLED);<🎜> <🎜>imageline($im, 0, $y2, 60, $y4, IMG_COLOR_STYLED)<🎜> <🎜> // Randomly generate a large number of black dots on the canvas to interfere; <🎜> <🎜>for($i=0;$i<80;$i++) {<🎜> <🎜>imagesetpixel($im, rand(0,60), rand(0,20), $black); }<🎜> <🎜>//Display four numbers randomly on the canvas, and the horizontal spacing and position of the characters are randomly generated according to a certain fluctuation range<🎜> <🎜>$strx=rand(3,8);<🎜> <🎜>for($i=0;$i<4;$i++){<🎜> <🎜>$strpos=rand(1,6); imagestring($im,5,$strx,$strpos, substr($num,$i,1), $black); $strx+=rand(8,12) ;<🎜> <🎜>}<🎜> <🎜>ImagePNG($im); ImageDestroy($im);<🎜> <🎜>?> On the reg.php page we write a form: (Other HTML code is omitted here)
The code is as follows Copy code
Verification code:
Verification code:

Because we submitted using post, we use $_POST to obtain it (verify the verification code on the acceptance page: post.php page)

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

Session_start();

//back_alert()验证码输入错误的时候,弹出错误信息

function back_alert($yzm){

echo “”;

}

//禁止恶意调用(禁止直接在浏览器打开post.php页面)

if($_POST["yzm"]==null){

back_alert(‘你都木有输入验证码,有木有???’);  }

// 禁止恶意注册

if(!($_POST["yzm"]==$_SESSION["Checknum"])){

back_alert(‘验证码不正确’);

} echo $_POST["yzm"];

Copy code
Session_start();

//back_alert() When the verification code is entered incorrectly, an error message will pop up

echo “”; } // Malicious calls are prohibited (it is prohibited to open the post.php page directly in the browser) if($_POST["yzm"]==null){ back_alert(‘You haven’t entered the verification code, have you???’); }
// Malicious registration is prohibited if(!($_POST["yzm"]==$_SESSION["Checknum"])){
back_alert(‘Verification code is incorrect’); } echo $_POST["yzm"]; http://www.bkjia.com/PHPjc/632625.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632625.htmlTechArticleA commonly used method to prevent malicious registration is to use verification codes. When the user submits registration information, I randomly generate a graphic. Verification code, so that only people can recognize it, of course simple verification...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template