PHP Generate English word captcha program code _php Tutorial

Source: Internet
Author: User
If you want to generate a word verification code we must have a well-done word library, the following I have prepared a TXT file, which put a lot of words, we just create a random read to the birth achievement ko.

Specific

The code is as follows Copy Code

$width = 145;
$height = 45;

$authcode = Vcaptcha_read_code (' words.txt ');

$BG = ' bg/captcha_bg3.jpg ';

$img _type = ' png ';

/* Verification Code length */
$letters = strlen ($authcode);

$img _BG = (function_exists (' Imagecreatefromjpeg ') && ((Imagetypes () & Img_jpg) > 0))?
Imagecreatefromjpeg ($BG): Imagecreatefromgif ($BG);
$BG _width = imagesx ($img _bg);
$BG _height = Imagesy ($img _bg);

$img _org = ((function_exists (' Imagecreatetruecolor ')) && php_version >= ' 4.3 ')?
Imagecreatetruecolor ($width, $height): Imagecreate ($width, $height);

/* Copy the original image and resize the background image */
if (function_exists (' imagecopyresampled ') && php_version >= ' 4.3 ')//GD 2.x
{
Imagecopyresampled ($img _org, $img _bg, 0, 0, 0, 0, $width, $height, $bg _width, $BG _height);
}
else//GD 1.x
{
Imagecopyresized ($img _org, $img _bg, 0, 0, 0, 0, $width, $height, $bg _width, $BG _height);
}
Imagedestroy ($img _BG);

$CLR = Imagecolorallocate ($img _org, 255, 255, 255);

/* Draw Border */
Imagerectangle ($img _org, 0, 0, $width-1, $height-1, $CLR);

/* Get the height and width of the verification code */
$x = ($width-(Imagefontwidth (5) * $letters))/2;
$y = ($height-imagefontheight (5))/2;
Imagestring ($img _org, 5, $x, $y, $authcode, $CLR);

Header (' Expires:thu, 1970 00:00:00 GMT ');

http/1.1
Header (' Cache-control:private, No-store, No-cache, Must-revalidate ');
Header (' Cache-control:post-check=0, pre-check=0, max-age=0 ', false);

http/1.0
Header (' Pragma:no-cache ');
if ($img _type = = ' jpeg ' && function_exists (' imagecreatefromjpeg '))
{
Header (' Content-type:image/jpeg ');
Imageinterlace ($img _org, 1);
Imagejpeg ($img _org, FALSE, 95);
}
Else
{
Header (' content-type:image/png ');
Imagepng ($img _org);
}

Imagedestroy ($img _org);

function Vcaptcha_read_code ($wordlist _file)
{
$fp = @fopen ($wordlist _file, ' RB ');
if (! $fp) return false;

$fsize = filesize ($wordlist _file);
if ($fsize <) return false; Too small of a list to be effective

if ($fsize < 128) {
$max = $fsize; Still pretty small but changes the range of seeking
} else {
$max = 128;
}

Fseek ($FP, rand (0, $fsize-$max), seek_set);
$data = Fread ($fp, 128); Read a random bytes from file
Fclose ($FP);
$data = Preg_replace ("/r?n/", "n", $data);

$start = Strpos ($data, "n", rand (0, 100)) + 1; Random Start position
$end = Strpos ($data, "n", $start); Find end of Word

Return Strtolower (substr ($data, $start, $end-$start)); Return substring in bytes
}

How to use:

The code is as follows Copy Code

Copy this code into the HTML and it's ready. And this getcode function is to let the user see the verification code, click can switch, JavaScript script is as follows:

Http://file.bKjia.c0m/upload/mbdown/pic/2013/06/26/13659538282901.txt This is the word library that everyone can download.

http://www.bkjia.com/PHPjc/629617.html www.bkjia.com true http://www.bkjia.com/PHPjc/629617.html techarticle if you want to generate a word verification code we must have a well-done word library, the following I have prepared a TXT file, which put a large number of words, we just generate when the ...

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