The perfect php generation CAPTCHA Code _php tutorial

Source: Internet
Author: User
This article introduces a very good code generation PHP program, first when the user requests HTTP, the server side creates a unique SessionID, this is the session ID, and then to start the GD library or ImageMagick these drawing tools, The random characters generated by the program are written into a picture, then displayed to the client, and then by the user input submitted data, and then our program to save the build validation in the session to compare, so that the verification code to complete the generation and verification.

• Create a new PHP file captcha_code_file.php

The code is as follows Copy Code

First Open session
Session_Start ();
Define the foreground display verification code length & width
$image _width = 120;
$image _height = 40;
$characters _on_image = 6;
$font = './monofont.ttf ';

The characters that can is used in the CAPTCHA code.
Avoid confusing characters (l 1 and I for example)
$possible _letters = ' 23456789bcdfghjkmnpqrstvwxyz ';
$random _dots = 10;
$random _lines = 30;
$captcha _text_color= "0x142864";
$captcha _noice_color = "0x142864";
Define a string to generate a verification code
$code = ";

$i = 0;
while ($i < $characters _on_image) {
$code. = substr ($possible _letters, Mt_rand (0, strlen ($possible _letters)-1), 1);
$i + +;
}

$font _size = $image _height * 0.75;
$image = @imagecreate ($image _width, $image _height);

/* Setting the background, text and noise colours here */
$background _color = imagecolorallocate ($image, 255, 255, 255);

$arr _text_color = Hexrgb ($captcha _text_color);
$text _color = imagecolorallocate ($image, $arr _text_color[' red '),
$arr _text_color[' green '], $arr _text_color[' Blue ');

$arr _noice_color = Hexrgb ($captcha _noice_color);
$image _noise_color = imagecolorallocate ($image, $arr _noice_color[' red '),
$arr _noice_color[' green '], $arr _noice_color[' Blue ');

/* Generating the dots randomly in background */
for ($i =0; $i < $random _dots; $i + +) {
Imagefilledellipse ($image, Mt_rand (0, $image _width),
Mt_rand (0, $image _height), 2, 3, $image _noise_color);
}

/* generating lines randomly in background of image */
for ($i =0; $i < $random _lines; $i + +) {
Imageline ($image, Mt_rand (0, $image _width), Mt_rand (0, $image _height),
Mt_rand (0, $image _width), Mt_rand (0, $image _height), $image _noise_color);
}

/* Create a text box and add 6 letters code in IT */
$textbox = Imagettfbbox ($font _size, 0, $font, $code);
$x = ($image _width-$textbox [4])/2;
$y = ($image _height-$textbox [5])/2;
Imagettftext ($image, $font _size, 0, $x, $y, $text _color, $font, $code);

/* Show CAPTCHA image in the page HTML page */
Header (' content-type:image/jpeg ');//Defining the image Type to being shown in browser widow
Imagejpeg ($image);//showing the image
Imagedestroy ($image);//destroying the image instance
Set session, do validation
$_session[' 6_letters_code '] = $code;

function Hexrgb ($HEXSTR)
{
$int = Hexdec ($HEXSTR);

Return Array ("Red" = 0xFF & ($int >> 0x10),
"Green" 0xFF & ($int >> 0x8),
"Blue" 0xFF & $int);
}

The call page displays the Captcha page index.php

The code is as follows Copy Code

Session_Start ();
if (Isset ($_request[' Submit ')) {
Code for check server side validation
if (Empty ($_session[' 6_letters_code ')) | |
STRCASECMP ($_session[' 6_letters_code '), $_post[' 6_letters_code '])! = 0)
{
$msg = "You entered the wrong verification code, please re-enter!";
}else{
echo "You typed it right!";
Captcha verification is Correct. Final Code Execute here!
}
}
?>


http://www.bkjia.com/PHPjc/632957.html www.bkjia.com true http://www.bkjia.com/PHPjc/632957.html techarticle This article introduces a very good code generation PHP program, first, when the user requests HTTP, the server side creates a unique SessionID, this is session sessions ...

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