Verification code for PHP development
Many websites will have a verification code when logging in. Below we will introduce how to implement the verification code function.
There is this section in the code of the previous login page:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"> <meta name="format-detection" content="telephone=no" /> <title>登录页面实例展示</title> <link rel="stylesheet" type="text/css" href="/phpMyAdmin/login.css"/> </head> <body> <p> <span> <input type="text" name="code" id="code" class="pf_ipt_verify w230" placeholder="验证码" autocomplete="off" tabindex="3"/> <img src="/phpMyAdmin/code.php" onClick="this.src='/phpMyAdmin/code.php?nocache='+Math.random()" style="cursor:hand"> </span> </p> </body> </html>
This is the picture of the verification code we see on the page. By giving the verification code image a click event, you can click the image once and change different numbers.
In addition, how the verification code is generated is the following code:
<?php
session_start();
Header("Content-type:image/PNG");
$im = imagecreate(150,45);
$back = imagecolorallocate($im, 245, 245, 245);
imagefill($im, 0,0, $back);
$vcodes = "";
for($i = 0; $i < 4; $i++){
$font = imagecolorallocate($im, rand(100, 255), rand(0, 100), rand(100, 255));
$authnum = rand(0, 9);
$vcodes .= $authnum;
imagestring($im, 5, 50 + $i * 10, 20, $authnum, $font);
}
$_SESSION['VCODE'] = $vcodes;
for($i=0;$i<200;$i++) {
$randcolor = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
imagesetpixel($im, rand()%150, rand()%150, $randcolor); //
}
imagepng($im);
imagedestroy($im);
?>This is the background program for code generation.
Judge the code in the login back-end code. One is to judge whether there is input, and the other is to judge whether the input is correct. The code is as follows:
if(!$_POST['code']){
echo('验证码不能为空');
return;
}else if($_POST['code']!=$_SESSION['VCODE']){
echo('验证码不正确');
return;
}- Course Recommendations
- Courseware download
The courseware is not available for download at the moment. The staff is currently organizing it. Please pay more attention to this course in the future~ 















