搜尋
首頁php教程php手册PHP 生成英文单词验证码程序代码

PHP 生成英文单词验证码程序代码

Jun 13, 2016 am 10:12 AM
php程式碼單字我們產生程式英語驗證

如果你要生成单词验证码我们必须一个己经做好的单词库了,下面我给大家准备了一个txt文件,这里面放了大量的单词,我们只要生成时随便读到生成就KO了。

具体

 代码如下 复制代码

$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         
    if ($fsize         $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
}

使用方法:

 代码如下 复制代码

PHP脚本生成单词验证码

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

http://file.bKjia.c0m/upload/mbdown/pic/2013/06/26/13659538282901.txt  这是单词库大家可下载。

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),