PHP Verification code detailed generation and use method

Source: Internet
Author: User
Tags php tutorial rand

Note: The following code needs to open the PHP tutorial of the GD library, modify the configuration of the Php.in file, the already commented out of the line before the semicolon cancellation: Extension=php_gd2.dll.

$width = 165;
$height = 120;
$image = Imagecreatetruecolor ($width, $height);
$BG _color = imagecolorallocate ($image, 255,255,255);
$tm = Imagecolorallocate ($image, 255,0,0);
Imagefilledrectangle ($image, 0,0, $width, $height, $BG _color);
Imagefill ($image, 0,0, $BG _color);
/*
$text = Random_text (5);
$text = "FFFF";
$font = 8;
$struft =iconv ("GBK", "Utf-8", $text);
$x = Imagesx ($image);
$y = Imagesy ($image);
$FG _color = imagecolorallocate ($image, 233,14,91);
Imagestring ($image, $font, $x, $y, $struft, $FG _color);
$_session[' captcha ' = $text;
*/
Header ("Content-type:image/png");
Imagepng ($image);
Imagedestroy ($image);

Example Two

validate.php

The use of session recognition, slightly improved the current network of PHP verification code, add clutter, digital color display randomly, control 4-digit digital display;

?
Session_Start ();
Generate Captcha Picture
Header ("Content-type:image/png");
$im = Imagecreate (44,18);
$back = Imagecolorallocate ($im, 245,245,245);
Imagefill ($im, 0,0, $back); Background

Srand (Double) microtime () *1000000);
Generate 4-digit numbers
For ($i =0 $i <4; $i + +) {
$font = Imagecolorallocate ($im, Rand (100,255), Rand (0,100), Rand (100,255));
$authnum =rand (1,9);
$vcodes. = $authnum;
Imagestring ($im, 5, $i *10, 1, $authnum, $font);
}

For ($i =0 $i <100; $i + +)//Add interference pixels
{
$randcolor = Imagecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255));
Imagesetpixel ($im, Rand ()%70, Rand ()%30, $randcolor);
}
Imagepng ($im);
Imagedestroy ($im);

$_session[' vcode ' = $vcodes;
?>

See the full example below

<?php

@header ("content-type:text/html; Charset=utf-8 ");

Open session

Session_Start ();

?>

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>

<title>php Verification code Sample </title>

<body>

Verification Code:<br/>

<iframe id= "iimg" height= "width=300 src=" img.php "0" ></iframe>

<br/>

<input Type=button value= "See not clear, change a" onclick= "iimg.location.reload ();" >

<br>

<form action= "validate.php" method= "POST" >

Input verification Code: <input name= "Imgid" style= "width:60" >

<input type= "Submit" value= "OK" >

</form>

</body>

PHP Determines whether the authentication code entered by the user is consistent with the system generation

<?php @header ("content-type:text/html; Charset=utf-8 ");

   //Open session

    session_start ();

   //Get user Input validation code and convert to uppercase

    $imgid _req = $_request[' Imgid '];

    $imgid _req = strtoupper ($imgid _req);

   //Verify that the string is registered with the session variable

    if (session_is_registered ($imgid _req)) {

       echo <font color=blue > Pass Validation! </font> ";

   } else {

       echo "<font color=red > validation Error! </font> ";

   }

   //Close session to clear all registered variables

    Session_destroy ();

?

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.