PHP Verification code generation and validation Example _php tutorial

Source: Internet
Author: User
Verification code is a common feature in WEB2.0 now, such as registration, login or Message page, you need a registration code to verify the legitimacy of the current operator, we will see some sites do not have verification code, but that is more advanced verification, the following we look at the common code generation and use method.

11 Simple Verification Code examples

1.1 Picture showing the verification code

The code is as follows Copy Code

$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 "";
Exit
}

Example of verification code for 21 characters

2.1 Picture showing the verification code

The code is as follows Copy Code
$str = "Han", "word", "test", "certificate", "code"); 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

2.3 Define a check () function

The code is as follows Copy Code

See a complete example

PHP Verification code generation and invocation of the example, usually in the development of often used, record a bit.

1. Verification code Generation file code.php

The code is as follows Copy Code

!--?
Header ("Content-type:image/png");
//define header, declare picture file, preferably PNG, no copyright disturbance;
//Generate a new four-bit integer verification Code
Session_Start ();//open session;
Authnum_session = ";
str = ' abcdefghijkmnpqrstuvwxyz1234567890 ';
//define the numbers and letters used to display on the image;
L = strlen (str);//Get the length of the string;
//loop randomly extracts four-bit letters and numbers defined earlier;
for (i=1;i<=4;i++)
{
Num=rand (0,L-1);
//randomly extracting one digit at a time, from the first word to the maximum length of the string,
//minus 1 because the Intercept character starts from 0; Such 34 characters may be in the line;
Authnum_session.= Str[num]; The
//will be Fulianqi by the number of words to be four bits altogether;
}
Session_register ("authnum_session");
//Use session to do validation is also good; Register session, name is Authnum_session,
// Other pages can be called by _session["Authnum_session" if they contain the image
//

Generate a Captcha picture,
Srand (Double) microtime () *1000000);
im = Imagecreate (50,20);//Picture width and height;
The main use of black and white gray three colors;
Black = Imagecolorallocate (IM, 0,0,0);
White = Imagecolorallocate (IM, 255,255,255);
Gray = Imagecolorallocate (IM, 200,200,200);
Drawing a four-bit integer verification code into a picture
Imagefill (Im,68,30,gray);
If you do not have to interfere with the line, the comment is OK;
Li = Imagecolorallocate (IM, 220,220,220);
for (i=0;i<3;i++)
{//Add 3 lines of interference, or do not; Depending on the circumstances, as it may affect user input;
Imageline (Im,rand (0,30), Rand (0,21), Rand (20,40), Rand (0,21), Li);
}
The position of the character in the picture;
Imagestring (IM, 5, 8, 2, authnum_session, white);
for (i=0;i<90;i++)
{//Add interfering pixels
Imagesetpixel (IM, rand ()%70, Rand ()%30, Gray);
}
Imagepng (IM);
Imagedestroy (IM);
?>

The above code, refer to the following article:
PHP Image Verification Code
Example of PHP generated verification code
Generate a snowflake background verification code with PHP

2. Call the Verification code page sessionvalidate.php

code as follows copy code

Session_Start ();
At the beginning of the page to open the session,
Error_reporting (2047);
Session_destroy ();
Remove the session to get a new session value each time;
With seesion effect is good, also very convenient
?>


Session Picture Verification Example


This example validates an instance of the session

Print the previous session;
echo "Last SESSION:". _session["Authnum_session"]. "
";
Validate= "";
if (Isset (_post["Validate")) {
validate=_post["Validate"];
echo "You have just entered:". _post["Validate"]. "
Status: ";
if (validate!=_session["Authnum_session"]) {
Determine if the session value is consistent with the user input verification code;
echo "Incorrect input";
}else{
echo "Pass verification";
}
}
/*
Print all session;
Printarr (_session);
function Printarr (Aarray) {
Echo '

'; &amp;lt;br/&amp;gt; Print_r (aarray); &amp;lt;br/&amp;gt; Echo ' ;
}
*/
?>

http://www.bkjia.com/PHPjc/632824.html www.bkjia.com true http://www.bkjia.com/PHPjc/632824.html techarticle Verification Code is a common feature in WEB2.0 now, such as registration, login or message page, need registration code to verify the legitimacy of the current operator, we will see some nets ...

  • Related Article

    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.