PHP instance: PHP generates a Web site verification code with a snowflake background

Source: Internet
Author: User
Tags define rand

<?session_start ();? >
<form method=post action= ">
<input type=text name=number maxlength=4>
<input type=" Submit "Name=" sub ">
</FORM>
.
//Validation code
if (isset ($HTTP _post_vars["sub")):
if ($HTTP _post_vars["number"]!= $HTTP _session_vars[login_ Check_number] | | Empty ($HTTP _post_vars["number")) {
    echo "Check code is incorrect!";
}else{
    echo Verification code passed! ";
}
EndIf;
Show_source (' test.php ');
//above this page's source code


The following is the source code to generate the verification code
Show_source (' yanzhengma.php ');
?>
<?php
Session_Start ();
Session_register ("Login_check_number");
Last night saw the verification code effect on the Chianren, just consider it, using PHP's GD library to complete a similar function
First genetic the background, and then put the generated verification into the stacking
$img _height=120; First define the length and width of the picture
$img _width=40;
if ($HTTP _get_vars["act"]== "init") {
Srand (Microtime () * 100000); After//php420, Srand is not necessary.
For ($Tmpa =0 $Tmpa <4; $Tmpa + +) {
$nmsg. =dechex (rand (0,15));
}//by Sports98


$HTTP _session_vars[login_check_number] = $nmsg;

$HTTP _session_vars[login_check_number] = Strval (Mt_rand ("1111", "9999")); Generates a 4-bit random number and puts it in session
Who can make the supplement, can generate letters and numbers at the same time?? ----finished by Sports98.

    $aimg = imagecreate ($img _height, $img _width);   //Generate picture
    Imagecolorallocate ($aimg, 255,255,255);           //Picture background , Imagecolorallocate 1th time to define color PHP is considered to be the background of the
    $black = imagecolorallocate ($aimg, 0,0,0);        //define required black
    imagerectangle ($aimg, 0,0, $img _height-1, $img _ Width-1, $black);//First into a black rectangle to surround the picture

   //below this generates a snowflake background, which is actually generating some symbols on the picture
    for ($i =1; $i <=100; $i + +) {    //First Test with 100
        imagestring ($aimg, 1,mt_rand (1, $img _height), Mt_ Rand (1, $img _width), "*", Imagecolorallocate ($aimg, Mt_rand (200,255), Mt_rand (200,255), Mt_rand (200,255)));
       /ha, see it, in fact, is not a snowflake, is the generation of * No. In order for them to look "cluttered, 5-color, 6", they have to have their position, color, or even size randomly counted at 1 1, and rand () or mt_rand can do it.
   }

With the background generated, it's time to put the generated random numbers up. The reason is similar to above, random number 1 1 places, at the same time let their position, size, color are used into random number ~ ~
In order to distinguish it from the background, the color here is not more than 200, the above is not less than 200
For ($i =0 $i <strlen ($HTTP _session_vars[login_check_number]); $i + +) {
Imagestring ($aimg, Mt_rand (3,5), $i * $img _height/4+mt_rand (1,10), Mt_rand (1, $img _width/2), $HTTP _session_vars[login _check_number][$i],imagecolorallocate ($aimg, Mt_rand (0,100), Mt_rand (0,150), Mt_rand (0,200));
}
Header ("Content-type:image/png"); Tell the browser that the following data is a picture, not a text display
Imagepng ($AIMG); Generate PNG format ... The effect is quite like a matter of ...
Imagedestroy ($AIMG);
}

?>



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.