How to implement php verification code

(*-*)浩
Release: 2023-02-24 07:06:01
Original
6583 people have browsed it

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.

How to implement php verification code

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=&#39;&#39;;
 
   //验证码为随机四个数字
    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[&#39;authcode&#39;] = $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(&#39;content-type:image/png&#39;);
	imagepng($image);
 
    //销毁
    imagedestroy($image);
Copy after login

View the effect as follows: refresh once, the content may change once

How to implement php verification code

Create a new form.php file and write the following code . Implementation verification:

<?php
 
	if (isset($_REQUEST[&#39;authcode&#39;])) {
		session_start();
 
		if (strtolower($_REQUEST[&#39;authcode&#39;])==$_SESSION[&#39;authcode&#39;]) {
			echo&#39;<font color ="#0000CC"> 输出正确</font>&#39;;
			# code...
		}else{
			echo $_REQUEST[&#39;authcode&#39;];
			echo $_SESSION[&#39;authcode&#39;];
			echo&#39;<font color ="#CC0000"> 输出错误</font>&#39;;
		}
 
		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(&#39;captcha_img&#39;).src=&#39;./captcha.php?r=&#39;+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>
Copy after login

The implementation results are as follows. Click to change one, and a verification code will be changed.

How to implement php verification code

#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!

Related labels:
php
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