Home >Backend Development >PHP Problem >How to implement php verification code
Verification code is the abbreviation of the fully automatic Turing test that distinguishes computers and humans. It is a public fully automatic program that distinguishes the user as a computer and a human.
The main application scenarios of verification code: before logging in and registering, before posting and replying to information, and when a suspected machine request is made, human/machine verification is performed.
Implementation steps:
(1) Generate base map;
Depends on PHP image processing library GD
(2) Generate verification content; (Recommended learning: PHP programming from entry to proficiency)
To generate random numbers, use the PHP function rand();
(3) Generate verification code image;
(4) Verify verification content
Requires PHP to operate SESSION basics, Save the verification content on the server side; front-end Ajax basics
Code:
Create a new captcha.php file and write the following code. Implement the verification code picture:
<?php //必须至于顶部,多服务器端记录验证码信息,便于用户输入后做校验 session_start(); //默认返回的是黑色的照片 $image = imagecreatetruecolor(100, 30); //将背景设置为白色的 $bgcolor = imagecolorallocate($image, 255, 255, 255); //将白色铺满地图 imagefill($image, 0, 0, $bgcolor); //空字符串,每循环一次,追加到字符串后面 $captch_code=''; //验证码为随机四个数字 for ($i=0; $i < 4; $i++) { $fontsize=6; $fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120)); //产生随机数字0-9 $fontcontent = rand(0,9); $captch_code.= $fontcontent; //数字的位置,0,0是左上角。不能重合显示不完全 $x=($i*100/4)+rand(5,10); $y=rand(5,10); imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor); } $_SESSION['authcode'] = $captch_code; //为验证码增加干扰元素,控制好颜色, //点 for ($i=0; $i < 200; $i++) { $pointcolor = imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200)); imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor); } //为验证码增加干扰元素 //线 for ($i=0; $i < 3; $i++) { $linecolor = imagecolorallocate($image,rand(80,220),rand(80,220),rand(80,220)); imageline($image, rand(1,99), rand(1,29),rand(1,99), rand(1,29) ,$linecolor); } header('content-type:image/png'); imagepng($image); //销毁 imagedestroy($image);
View the effect as follows: refresh once, the content may change once
Create a new form.php file and write the following code . Implementation verification:
<?php if (isset($_REQUEST['authcode'])) { session_start(); if (strtolower($_REQUEST['authcode'])==$_SESSION['authcode']) { echo'<font color ="#0000CC"> 输出正确</font>'; # code... }else{ echo $_REQUEST['authcode']; echo $_SESSION['authcode']; echo'<font color ="#CC0000"> 输出错误</font>'; } exit(); } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>确认验证码</title> </head> <body> <form method="" ="post" action="./form.php"> <p>验证码图片: <img id="captcha_img" border="1" src="./captcha.php?r=<?php echo rand(); ? alt="How to implement php verification code" >" alt="" width="100" style="max-width:90%"> <a href="javascript:void(0)" οnclick="document.getElementById('captcha_img').src='./captcha.php?r='+Math.random() ">换一个?</a> </p> <p>请输入图片中的内容: <input type="text" name="authcode" value="" /> </p> <p> <input type="submit" value="提交" style="padding: 6px 20px;"> </p> </form> </body> </html>
The implementation results are as follows. Click to change one, and a verification code will be changed.
#Enter the content in the picture. If it is correct, it will prompt you to enter it correctly. If it is wrong, it will prompt you to input it incorrectly. Modify the style yourself.
The above is the detailed content of How to implement php verification code. For more information, please follow other related articles on the PHP Chinese website!