PHP How to implement login verification code check function

Source: Internet
Author: User
This article is mainly for everyone to introduce the PHP implementation verification Code check function, with a certain reference value, interested in small partners can refer to

Verification code is implemented using the session function in PHP.
Declare the function session_start () at the top. Tell the server that we want to use this function.


Session_Start ();


The next thing we use is the code implemented by the verification code. Here is an example of the code for the English numerals.


$image = Imagecreatetruecolor (100, 30); Create a 100x30 Canvas $white = imagecolorallocate ($image, 255,255,255);//White Imagefill ($image, 0,0, $white);//Cover Black canvas


Then declare an empty variable before the verification Code implementation to hold the verification code.


$session = ""; An empty variable that holds the verification code for ($i =0; $i <4; $i + +) {   $size = 6;   $x = $i *25+mt_rand (5,10);   $y = Mt_rand (5,10);   $sizi _color = imagecolorallocate ($image, Mt_rand (80,220), Mt_rand (80,220), Mt_rand (80,220));   $char = Join ("", Array_merge (Range (' A ', ' Z '), Range (' A ', ' Z '), Range (0,9)));   $char = Str_shuffle ($char);   $char = substr ($char, 0,1);   Imagestring ($image, $size, $x, $y, $char, $sizi _color);   $session. = $char; Assign each value of the verification code to the variable}   $_session[' SESSION '] = $session;//The value of this variable is equal to the value entered by the user



for ($k =0; $k <200; $k + +) {   $rand _color = imagecolorallocate ($image, Mt_rand (50,200), Mt_rand (50,200), Mt_rand ( 50,200));   Imagesetpixel ($image, Mt_rand (1,99), Mt_rand (1,29), $rand _color); } for  ($n =0; $n <5; $n + +) {   $line _color = imagecolorallocate ($image, Mt_rand (80,220), Mt_rand (80,220), Mt_ Rand (80,220));   Imageline ($image, Mt_rand (1,99), Mt_rand (1,29), Mt_rand (1,99), Mt_rand (1,29), $line _color); }  header (' content-type:image/png ');//Set file output format imagepng ($image);//Export $image image Imagedestroy ($image) in PNG format; Destroying images


Use POST to receive the verification code. The Strtolower function is to make the server case insensitive. This can effectively reduce the user's error rate.


if (Isset ($_post[' Session '))) {   session_start ();   if (Strtolower ($_post[' Session ')) ==strtolower ($_session[' Session ')) {     echo ' <font color= ' #0000CC ' > input correct </form> ';   } else{     Echo ' <font color= "#CC0000" ><b> input error </b></font> ';   }   Exit (); }


The following is the HTML page code.


<! DOCTYPE html> 

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.