PHP implementation of verification Code steps and service-side check code

Source: Internet
Author: User
This article introduces to you about PHP implementation Verification code steps and service-side check code, there is a certain reference value, the need for a friend can refer to, I hope you have some help.

What is a CAPTCHA: A verification code is a public program that distinguishes whether a user is a computer or a person

Four steps required to make a verification code

1: Generate Basemap

2: Generate validation content

3: Generate Verification Code content

4: Verifying content

Step first, create a basemap:

Goal: Generate a 100*30-sized picture from PHP

Methods: Imagecreatetruecolor ($width, $height);

Caveats: Rely on GD extensions

Before outputting the image, the header information of the image must be printed in advance--"Send native HTTP header

The method defaults to output a black background

Imagecreatetruecolor () A new true color image is represented by a $image, which is then heavily used

Since it is to create a true color image, it will have a variety of colors, the following imagecolorallocate (choose the canvas, three-color parameters)

What do you want to fill imagefill (choose Canvas, start position, color)

To this, a basemap is generated, and the following begins with adding seasoning

$image = Imagecreatetruecolor (100,30)

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

Imagefill ($image, 0,0, $bgcolor)

Step two: Generate validation content

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

Method: Through the loop, imagestring function, horizontally generate a line of string (according to the imagestring inside the parameter position, to fill in)

Precautions: Control the font size, n/n

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

The variable is defined here according to the parameters inside the imagestring, and the variable is assigned a value

Imagestring ($image, $fontsze, $x, $y, $fontcontent, $fontcolor)

}

$fontcontent = substr ($data, Rand (0,strlen ($DATA)), 1);

If you want a combination of numbers and letters, the Substr method means that the substring of the string is returned, and the returned string gets the data randomly, starting with a maximum of 1 lengths

The third step is to generate the verification code content

Objective: To increase the interference element for the verification code, the interference element is a point or a line

Method: Imagesetpixel Point, imageline-Line (resource file, start position, color)

Precautions: Interference elements must control the color and quantity, avoid distracting

Fourth Step: Verify information through session storage

Target: Make record on server side, easy for user to enter verification code to do check

Method: Session_Start ()

Caveats: session_start () must be at the top of the script

Under multi-service situations, we should consider centralized management of session management

Imagepng exporting a picture to a browser or file in PNG format

Imagedestroy Destroying pictures good habit

In these methods, the use of resources is very much, that is, every method to $image this canvas

<php? $image = Imagecreatetruecolor (100,30), $bgcolor = Imagecolorallocate ($image, 255,255,255); Imagefill ($image, 0,0, $bgcolor);//for ($i =0; $i <4; $i + +) {//$fontsize = 6;//$fontcolor = imagecolorallocate ($image, Rand (0,120), Rand ( 0,120), Rand (0,120)),//$fontcontent = rand (0,9),//$x = ($i *100/4) +rand (5,10);//$y = rand (5,10);//Imagestring ($image, $    FontSize, $x, $y, $fontcontent, $fontcolor)//} $captch _code= "; for ($i =0; $i <4; $i + +) {$fontsize = 6;    $fontcolor = Imagecolorallocate ($image, Rand (0,120), Rand (0,120), Rand (0,120));    $data = ' abcdefhijkimnpqrstuvwxy345678 ';     $fontcontent = substr ($data, Rand (0,strlen ($DATA)), 1);    $captch _code.= $fontcontent;    $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,99), $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,99), Rand (1,99), Rand (1,99), $pointcolor);} Header (' content-type:image/png '); Imagepng ($image);//end;imagedestroy ($IAMGE);? >

Verification code is made here, next is on the server side, do the calibration

Src= "captcha-2.php?r=<?php echo rand ();? > "   It's no big deal for this r to find information.

<image border= ' 1 ' src= ' captcha-2.php ' onclick= "this.src= ' captcha.php?t= ' + math.random ()" title= "Click to refresh"/> It's meant to be.

He still uses t here, so R ah T

For capitalization, use Strtolower () to convert all uppercase letters entered by the user into lowercase letters.

<?phpif (isset ($_request[' code ')) {    session_start ();    if (Strtolower ($_request[' code ')) {==$_session[' code '])    {        header (' content-type:text/html; Charset=utf8 ');        Echo ' 

Related articles recommended:

How to convert TXT file content to an array and get the specified content by number of lines in PHP (example)

How PHP uses latitude and longitude to calculate the distance between two points (pure code)

How PHP removes code instances for all files in directories and directories

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.