Php uses the verification code to prevent malicious registration of Study Notes

Source: Internet
Author: User

Php uses the verification code to prevent malicious registration of Study Notes

Today, we will study the PHP verification code. We will implement it through a simple digital verification code. First, we will write a code to generate the verification code:

The Code is as follows:  

<? Php

// Generate a 4-digit Verification Code randomly

$ Num = ""; for ($ I = 0; $ I <4; $ I ++) {$ num. = rand (0, 9 );}

// The four-digit verification code can also be directly generated using rand (,)

// Write the generated verification code to the session, which is used on the standby verification page.

Session_start (); $ _ SESSION ["Checknum"] = $ num;

// Create an image and define the color value Header ("Content-type: image/PNG ");

Srand (double) microtime () * 1000000 );

$ Im = imagecreate (60, 20 );

$ Black = ImageColorAllocate ($ im, 0, 0 );

$ Gray = ImageColorAllocate ($ im, 200,200,200 );

Imagefill ($ im, 0, 0, $ gray );

// Randomly draw two dotted lines to interfere

$ Style = array ($ black, $ gray, $ gray );

Imagesetstyle ($ im, $ style );

$ Y1 = rand (); $ y2 = rand (); $ y3 = rand (); $ y4 = rand );

Imageline ($ im, 0, $ y1, 60, $ y3, IMG_COLOR_STYLED );

Imageline ($ im, 0, $ y2, 60, $ y4, IMG_COLOR_STYLED)

// Randomly generate a large number of black spots on the canvas, which can interfere with the canvas;

For ($ I = 0; $ I <80; $ I ++ ){

Imagesetpixel ($ im, rand (0, 60), rand (0, 20), $ black );}

// Display the four numbers randomly on the canvas. The horizontal spacing and position of the characters are randomly generated according to a certain fluctuation range.

$ Strx = rand (3, 8 );

For ($ I = 0; $ I <4; $ I ++ ){

$ Strpos = rand (1, 6); imagestring ($ im, 5, $ strx, $ strpos, substr ($ num, $ I, 1), $ black ); $ strx + = rand (8, 12 );

}

ImagePNG ($ im); ImageDestroy ($ im );

?>

On the reg. php page, write a form: (Other HTML code is saved here)

The Code is as follows:  

<Tr>

<Td> Verification Code: </td>

<Td> <input type = "text" name = "yzm" style = "width: 60px; height: 20px;"/> </img> </td>

</Tr>

<Tr> <td colspan = '2'> <input type = "submit" value = "register"/> </td>

<Td> Verification Code: </td>

</Tr>

Because we use post to submit the request, we use $ _ POST to obtain the request (verify the verification code on the acceptance page: post. php page)

The Code is as follows:  

Session_start ();

// The error message is displayed when the back_alert () Verification code is incorrect.

Function back_alert ($ yzm ){

Echo "<script type = 'text/javascript '> alert (' $ yzm'); history. back (); </script> ";

}

// Prohibit malicious calls (prohibit direct access to the post. php page in the browser)

If ($ _ POST ["yzm"] = null ){

Back_alert ??? ');}

// Disable malicious Registration

If (! ($ _ POST ["yzm"] ==$ _ SESSION ["Checknum"]) {

Back_alert ('incorrect Verification Code ');

} Echo $ _ POST ["yzm"];

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.