PHP verification code generation and verification example

Source: Internet
Author: User

Verification code is a common feature in Web, such as registration, login, or message page. You need a registration code to verify the legality of the current operator. We will see that some websites do not have verification codes, but that is more advanced verification. Let's look at the common verification code generation and use methods.

1. A simple verification code instance

1.1 display the verification code picture

The Code is as follows: Copy code

<? Php
$ Num = intval (mt_rand (1000,9999 ));
For ($ I = 0; $ I <4; $ I ++)
{Echo " ";}
?>

1.2 verification process

The Code is as follows: Copy code

If (strval ($ inputyzm )! = Strval ($ num ))
{
Echo "<script> alert ('verification code is incorrect! '); History. go (-1); </script> ";
Exit;
}

2. Example of a Chinese character Verification Code

2.1 display the verification code picture

The Code is as follows: Copy code
<? Php
$ Str = "Han", "Word", "Verification", "certificate", "Code"); // you can define the content and number of Chinese characters.
$ Word = strlen ($ str ));
For ($ I = 0; $ I <4; $ I ++)
{
$ Num = rand (0, $ word );
$ Img = $ img. " ";
$ Pic = $ pic. $ str [$ num];
}
>

2.2 assign the generated random string to a hidden field

The Code is as follows: Copy code
<Input type = "hidden" name = "yzm" value = "<? Php echo $ pic ;? /> ">

2.3 define a check () function

The Code is as follows: Copy code

<Script language = "javascript">
Function check (form)
{
If (form. yzm. value = ""){
Alert ("Enter the Verification Code ");
Form. yzm. focus ();
Return false;
}
If (form. yzm. vale! = Form. yz. value)
{Alert ("Incorrect verification code ");
Form. yzm. focus ();
Return false;
}
}
</Script>

View a complete instance

Php verification code generation and call examples are frequently used during development. Record them.

1. File code. php generated by the Verification code

The Code is as follows: Copy code

<?
Header ("Content-type: image/png ");
// Define the header and declare the image file, preferably png, without copyright concerns;
// Generate a new four-digit integer Verification Code
Session_start (); // enable session;
Authnum_session = '';
Str = 'abcdefghijkmnpqrstuvwxyz1234567890 ';
// Define the numbers and letters used to display on the image;
L = strlen (str); // obtain the string length;
// Cyclically and randomly extract four letters and numbers defined above;
For (I = 1; I <= 4; I ++)
{
Num = rand (0 L-1 );
// A number is randomly selected at a time. From the first word to the maximum length of the string,
// Minus 1 is because the truncation character starts from 0, so that any 34 characters may be listed in it;
Authnum_session. = str [num];
// Connects the characters obtained from numbers to a total of four digits;
}
Session_register ("authnum_session ");
// It is good to use the session for verification. register the session and name it authnum_session,
// If other pages contain the image
// It can be called through _ SESSION ["authnum_session "]

// Generate the verification code image,
Srand (double) microtime () * 1000000 );
Im = imagecreate (50, 20); // image width and height;
// Black, white, and gray colors are used;
Black = ImageColorAllocate (im, 0, 0 );
White = ImageColorAllocate (im, 255,255,255 );
Gray = ImageColorAllocate (im, 200,200,200 );
// Print the four-digit integer verification code into the image
Imagefill (im, 68, 30, gray );
// Comment out the line without interfering with the line;
Li = ImageColorAllocate (im, 220,220,220 );
For (I = 0; I <3; I ++)
{// Add three interference lines; or do not; depending on the situation, because it may affect user input;
Imageline (im, rand (), rand (), rand (20, 40), rand (), li );
}
// The Position of the character in the image;
Imagestring (im, 5, 8, 2, authnum_session, white );
For (I = 0; I <90; I ++)
{// Add interference pixels
Imagesetpixel (im, rand () % 70, rand () % 30, gray );
}
ImagePNG (im );
ImageDestroy (im );
?>

For the above Code, refer to the following article:
Php image Verification Code
Php verification code generation example
Use php to generate a verification code with a snowflake background

2. sessionValidate. php

The Code is as follows: Copy code

<? Php
Session_start ();
// Enable the session first at the top,
// Error_reporting (2047 );
Session_destroy ();
// Remove the session to get the new session value every time;
// Use seesion for good results and convenience
?>
<Html>
<Head>
<Title> session image verification instance </title>
</Head>
<Body>
This example is a session verification instance.
<Form action = "" method = "post">
Verification Code: <input type = "text" name = "validate" value = "" size = 10> <br>
<Input type = "submit">
</Form>
<? Php
// Print the previous session;
Echo "previous session: <B>". _ SESSION ["authnum_session"]. "</B> <br> ";
Validate = "";
If (isset (_ POST ["validate"]) {
Validate = _ POST ["validate"];
Echo "you just entered:". _ POST ["validate"]. "<br> Status :";
If (validate! = _ SESSION ["authnum_session"]) {
// Determine whether the session value is consistent with the verification code entered by the user;
Echo "<font color = red> incorrect input </font> ";
} Else {
Echo "<font color = green> Verification passed </font> ";
}
}
/*
// Print all sessions;
PrintArr (_ SESSION );
Function PrintArr (aArray ){
Echo '<xmp> ';
Print_r (aArray );
Echo '</xmp> ';
}
*/
?>

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.