Home > Backend Development > PHP Tutorial > PHP generates English word verification code program code_PHP tutorial

PHP generates English word verification code program code_PHP tutorial

WBOY
Release: 2016-07-13 17:11:04
Original
1513 people have browsed it

If you want to generate a word verification code, we must have a prepared word library. Below I have prepared a txt file for everyone. There are a lot of words in it. We just need to read it casually when generating it and it will be 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 background image from the original image and resize it */
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, 01 Jan 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 < 32) 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 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); $end = strpos($data, "n", $start); // find end of word
                                   
Return strtolower(substr($data, $start, $end - $start)); // return substring in 128 bytes
}

Usage:

PHP script generates word verification code Copy this code into HTML and you’re done. There is also this getCode function to prevent users from seeing the verification code clearly. Click to switch. The JavaScript script is as follows:

http://file.bKjia.c0m/upload/mbdown/pic/2013/06/26/13659538282901.txt This is a vocabulary library that everyone can download.

http://www.bkjia.com/PHPjc/629617.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629617.htmlTechArticleIf you want to generate a word verification code, we must have a prepared word library. I will prepare it for you below. I have a txt file with a lot of words in it. We just need to generate it anytime...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
The code is as follows
代码如下 复制代码

PHP脚本生成单词验证码

将这段代码复制到HTML里,就可以了。还有这个的getCode函数是为了让用户看不清验证码,点击可以切换,JavaScript脚本如下:

Copy code