php 数学运算验证码实现代码

yipeiwu_com6年前PHP代码库
复制代码 代码如下:

<?php
//-------------------------------------
// 文件说明:数学运算验证码
// 文件作者:Jesse Lee
// 最后更新:2008-09-07
//-------------------------------------

session_start();

$sessionvar = 'vdcode'; //Session变量名称
$width = 150; //图像宽度
$height = 20; //图像高度

$operator = '+-*'; //运算符

$code = array();
$code[] = mt_rand(1,9);
$code[] = $operator{mt_rand(0,2)};
$code[] = mt_rand(1,9);
$code[] = $operator{mt_rand(0,2)};
$code[] = mt_rand(1,9);
$codestr = implode('',$code);
eval("\$result = ".implode('',$code).";");
$code[] = '=';

$_SESSION[$sessionvar] = $result;

$img = ImageCreate($width,$height);
ImageColorAllocate($img, mt_rand(230,250), mt_rand(230,250), mt_rand(230,250));
$color = ImageColorAllocate($img, 0, 0, 0);

$offset = 0;
foreach ($code as $char) {
$offset += 20;
$txtcolor = ImageColorAllocate($img, mt_rand(0,255), mt_rand(0,150), mt_rand(0,255));
ImageChar($img, mt_rand(3,5), $offset, mt_rand(1,5), $char, $txtcolor);
}

for ($i=0; $i<100; $i++) {
$pxcolor = ImageColorAllocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
ImageSetPixel($img, mt_rand(0,$width), mt_rand(0,$height), $pxcolor);
}

header('Content-type: image/png');
ImagePng($img);
?>

相关文章

php中将地址生成迅雷快车旋风链接的代码[测试通过]

在线演示地址:http://tools.jb51.net/tools/cs.php复制代码 代码如下:<?php function zhuanhuan() { $urlodd=ex...

PHP页面间参数传递的四种方法详解

我们定义page01.php和page02.php两个php文件,将page01中的内容想办法传递到page02,然后供我们继续使用。--------------------------...

PHP中读写文件实现代码

在PHP中读写文件,可以用到一下内置函数: 1.fopen(创建文件和打开文件) 语法: 复制代码 代码如下:fopen(filename,mode) filename,规定要打开的文...

WordPress中给文章添加自定义字段及后台编辑功能区域

WordPress中给文章添加自定义字段及后台编辑功能区域

add_post_meta add_post_meta 函数是 WordPress 中用来给文章或页面添加自定义字段值的一个函数, 其用法与在编写文章时在文章编写界面中利用自定义栏目面...

Windows下编译PHP5.4和xdebug全记录

实际上我最终目的是编译得到支持 PHP5.4 的 php_xdebug.dll,而在此之前,成功编译 PHP5.4 是必须的。 编译环境以及相关软件包: 1.Microsoft Visu...