PHP Generate verification Code and Verification Code Verification complete Example _php tutorial

Source: Internet
Author: User
This article to share a self-used verification code instance, from the generation of picture verification code to the use of examples of using verification code, there are students who need to learn to refer to this article Oh.

Hyml page

The code is as follows Copy Code





Untitled Document




The verifycode.php file code is as follows

The code is as follows Copy Code
/*
Image verification Code Powered by Kason
*/
Session_Start ();
$num =4;//number of verification codes
$width =80;//Verification Code width
$height =20;//Verification Code height
$code = ";
for ($i =0; $i < $num; $i + +)//Generate Verification code
{
Switch (rand (0,2))
{
Case 0: $code [$i]=CHR (rand (48,57)); break;//number
Case 1: $code [$i]=CHR (rand (65,90)); break;//Capital Letter
Case 2: $code [$i]=CHR (rand (97,122)); break;//Small Letter
}
}
$_session["Verifycode"]= $code;
$image =imagecreate ($width, $height);
Imagecolorallocate ($image, 255,255,255);
for ($i =0; $i <80; $i + +)//Generate interference pixels
{
$dis _color=imagecolorallocate ($image, Rand (0,2555), Rand (0,255), Rand (0,255));
Imagesetpixel ($image, rand (1, $width), rand (1, $height), $dis _color);
}
for ($i =0; $i < $num; $i + +)//print character to image
{
$char _color=imagecolorallocate ($image, Rand (0,2555), Rand (0,255), Rand (0,255));
Imagechar ($image, Max, ($width/$num) * $i, Rand (0,5), $code [$i], $char _color);
}
Header ("Content-type:image/png");
Imagepng ($image);//output image to browser
Imagedestroy ($image);//Release resources
?>


checkcode.php files are as follows

The code is as follows Copy Code


Ini_set (' display_errors ', ' Off ');
Session_Start ();
if ((Strtoupper ($_post["code")) = = Strtoupper (($_session["Verifycode"])) {
Print ("Verification code is correct,");
}else{
Print ("Captcha error,");
}
echo "submitted Captcha:". Strtoupper ($_post["code"]). ", correct captcha:". Strtoupper ($_session["Verifycode"]);
?>

http://www.bkjia.com/PHPjc/631291.html www.bkjia.com true http://www.bkjia.com/PHPjc/631291.html techarticle This article to share a self-used verification code instance, from the generation of picture verification code to the use of examples of using verification code, there are students who need to learn to refer to this article Oh. Hyml page ...

  • 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.