Home  >  Article  >  Backend Development  >  PHP steps to implement verification code and server-side verification code

PHP steps to implement verification code and server-side verification code

不言
不言Original
2018-08-04 15:23:142418browse

This article introduces to you the steps to implement verification code in PHP and the server-side verification code. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

What is the verification code: The verification code is a public program that distinguishes whether the user is a computer or a human.

It takes four steps to make a verification code

1: Generate a base map

2: Generate verification content

3: Generate verification code content

4: Verify verification content

First step by step, the first step, Generate base map:

Goal: Generate a 100*30 size image through php

Method: imagecreatetruecolor($width,$height);

Note Matters: Relying on GD extension

Before outputting a picture, the header information of that picture must be output in advance--》》Send native http header

This method outputs a black background by default

imagecreatetruecolor() Create a new true color image represented by $image. Later, it will be used a lot.

Since we are creating a true color image, we must have a variety of colors. Next, imagecolorallocate(select canvas, three colors Parameters)

What do you want to use to fill imagefill (select canvas, starting position, color)

With this, the base image is generated, let’s start adding some ingredients

$image = imagecreatetruecolor(100,30)

$bgcolor = imagecolorallocate($image,255,255,255);

imagefill($image,0,0,$bgcolor)

Step 2: Generate verification content

Goal: Randomly generate numbers (size, starting position, content, color)

Method: Generate a line of characters horizontally through loops and imagestring functions String (fill in according to the parameter position in imagestring)

Note: Control the font size, N/n

for($i=0;$ddfa6b5eaffff5c23ef18ebbf4463097"   对于这个r 找了资料,没什么大用

77d5becf1d39da023c84b2dbb6347e09 This is the original intention

He also uses t here, so r ah t ah

Taking into account the case, strtolower() is used here to convert all uppercase letters entered by the user into lowercase letters

<?php

if(isset($_REQUEST[&#39;code&#39;]))
{
    session_start();
    if (strtolower($_REQUEST[&#39;code&#39;])==$_SESSION[&#39;code&#39;])
    {
        header(&#39;Content-type: text/html; charset=UTF8&#39;);
        echo &#39;<h1 color="#0000CC">输入正确</h1>&#39;;
    }
    else{
        header(&#39;Content-type: text/html; charset=UTF8&#39;);
        echo &#39;<h1 color="#CC0000"><b>输入错误</b></h1>&#39;;
    }
    exit();
}

?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>确认验证</title>
</head>
<body>
<form method="post" action="form.php">
    <p>验证码图片:<img border="1" src="captcha-2.php?r=<?php echo rand();?>" width="100" height="30">

    </p>
    <p>请输入图片的内容:<input type="text" name="code" value=""/></p>
    <p><input type="submit" value="提交" style="padding:6px 20px;"></p>
</form>
</body>
</html>

Recommended related articles:

How to use PHP Convert the txt file content into an array and get the specified content by line number (example)

How does php use longitude and latitude to calculate the distance between two points (pure code)

php code example of how to delete a directory and all files in the directory

The above is the detailed content of PHP steps to implement verification code and server-side verification code. For more information, please follow other related articles on the PHP Chinese website!

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