PHP generated English word Verification Code program code

Source: Internet
Author: User

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 background image to the original image and adjust the size * *
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 is 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 128 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 128 bytes
}

How to use:

The code is as follows Copy Code

Copy this code into HTML and it's OK. There is also this GetCode function to allow users to see the authentication code, click can switch, JavaScript script is as follows:

<script type= "Text/javascript" >
function clickimg ()
{
var imgcode = "http://www.111cn.net/" +math.random ();
document.getElementById ("Captcha_img"). setattribute (' src ', imgcode);
}
</script>

Http://file.111cn.net/upload/mbdown/pic/2013/06/26/13659538282901.txt This is the word library you can download.

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.