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

yipeiwu_com5年前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);
?>

相关文章

Windows平台实现PHP连接SQL Server2008的方法

本文实例讲述了Windows平台实现PHP连接SQL Server2008的方法。分享给大家供大家参考,具体如下: 如果你需要和sql server通信需要到http://msdn.mi...

PHP和JavaScrip分别获取关联数组的键值示例代码

PHP版: 复制代码 代码如下: $o = array('x'=>1, 'y'=>2, 'z'=>3); $arr = array(); $i = 0; foreach...

php数组函数序列之each() - 获取数组当前内部指针所指向元素的键名和键值,并将指针移到下一位

each()定义和用法 each() 函数生成一个由数组当前内部指针所指向的元素的键名和键值组成的数组,并把内部指针向前移动。 返回的数组中包括的四个元素:键名为 0,1,key 和 v...

php保存信息到当前Session的方法

本文实例讲述了php保存信息到当前Session的方法。分享给大家供大家参考。具体如下: php中可通过$_SESSION保存session变量,下面的代码简单演示了 $_SESSION...

PHP读取word文档的方法分析【基于COM组件】

本文实例讲述了PHP读取word文档的方法。分享给大家供大家参考,具体如下: php开发 过程中可能会word文档的读取问题,这里可以利用com组件来完成此项操作 一、先开启php.in...