php生成随机生成不同的单词,PHP 生成英文单词验证码程序代码

$width=145;

$height = 45;

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

$bg = 'bg/captcha_bg3.jpg';

$img_type   = 'png';

/* 验证码长度 */

$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);

/* 将背景图象复制原始图象并调整大小 */

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);

/* 绘制边框 */

imagerectangle($img_org, 0, 0, $width - 1, $height - 1, $clr);

/* 获得验证码的高度和宽度 */

$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);           // find end of word

return strtolower(substr($data, $start, $end - $start)); // return substring in 128 bytes

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值