PHP Verification Code generation and verification example

Source: Internet
Author: User
Tags rand strlen

11 Simple example of a verification code

1.1 Picture of the verification code displayed

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 (' CAPTCHA error!) '); History.go ( -1);</script> ";
Exit
}

A verification code example for 21 Chinese characters

2.1 Picture of the verification code displayed

The code is as follows Copy Code
<?php
$str = "Han", "word", "examination", "proof", "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
<input type= "hidden" name= "Yzm" value= "<?php echo $pic;?/>" >

2.3 Define a check () function

  code is as follows copy code

     <script language= "javascript"
    function Check (form)
    {
    if (form.yzm.value== "") {
    alert ("Please enter a validation code");
    Form.yzm.focus ();
    return false;
   }
    if (form.yzm.vale!=form.yz.value)
    {alert ("Authenticode error");
     Form.yzm.focus ();
    return false;
   }
   }
    </script>

Look at a complete example

PHP Verification code generation and invocation of examples, usually in the development of the use, recorded.

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;
To generate a new four-bit integer verification code
Session_Start ()//open session;
Authnum_session = ';
str = ' abcdefghijkmnpqrstuvwxyz1234567890 ';
Define the numbers and letters to be displayed on the picture;
L = strlen (str); Gets the length of the string;
The loop randomly extracts the four-bit previously defined letters and numbers;
for (i=1;i<=4;i++)
{
Num=rand (0,L-1);
Randomly extract one digit at a time, from the first word to the maximum length of the string,
Minus 1 is because the Intercept character starts at 0, so that 34 characters may be in the line.
Authnum_session.= Str[num];
will be through the number of words Fulianqi to a total of four bits;
}
Session_register ("Authnum_session");
Use the session to do verification is also good; Register session, name is Authnum_session,
Other pages just include the picture
That is, you can call it by _session["Authnum_session"]

Generate a Captcha picture,
Srand (Double) microtime () *1000000);
im = Imagecreate (50,20);//Picture wide and high;
Mainly used in black and white gray three kinds of color;
Black = Imagecolorallocate (IM, 0,0,0);
White = Imagecolorallocate (IM, 255,255,255);
Gray = Imagecolorallocate (IM, 200,200,200);
To draw a four-bit integer verification code into a picture
Imagefill (Im,68,30,gray);
If you do not need to disturb the line, comments on the line;
Li = Imagecolorallocate (IM, 220,220,220);
for (i=0;i<3;i++)
{//Add 3 interference lines; it may not, depending on the circumstances, because it may affect user input;
Imageline (Im,rand (0,30), Rand (0,21), Rand (20,40), Rand (0,21), Li);
}
character in the position of the picture;
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);
?>

The above code, refer to the following article:
PHP Picture Verification Code
Example of PHP generating verification code
Generate a validation code with a snowflake background in PHP

2, call the Verification code page sessionvalidate.php

The code is as follows Copy Code

<?php
Session_Start ();
In the page first to open session,
Error_reporting (2047);
Session_destroy ();
The session is removed so that a new session value can be taken each time;
With seesion effect is good, also very convenient
?>
<title>session Picture Validation Example </title>
<body>
This example is the session validation instance
<form action= "" method= "POST" >
Verify code: <input type= "text" name= "Validate" value= "" Size=10> <br>
<input type= "Submit" >
</form>
<?php
Print the previous session;
echo "Prev session:<b>". _session["Authnum_session". " </b><br> ";
Validate= "";
if (Isset (_post["validate"])) {
validate=_post["Validate"];
echo "You just entered is:". _post["Validate"]. " <br> Status: ";
if (validate!=_session["Authnum_session"]) {
To determine whether the session value is consistent with the user's input validation code;
echo "<font color=red> input error </font>";
}else{
echo "<font color=green> through validation </font>";
}
}
/*
Print all sessions;
Printarr (_session);
function Printarr (Aarray) {
Echo ' <xmp> ';
Print_r (Aarray);
Echo ' </xmp> ';
}
*/
?>

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.