当前位置: 首页 > 图文教程 > 网络编程 > PHP > php 数学运算验证码实现代码

PHP
php 服务器调试 Zend Debugger 的安装教程
从Web查询数据库之PHP与MySQL篇
php 应用程序安全防范技术研究
php 不同编码下的字符串长度区分
php 生成饼图 三维饼图
PHP 字符截取 解决中文的截取问题,不用mb系列
PHP5 操作MySQL数据库基础代码
php面向对象全攻略 (一) 面向对象基础知识
php面向对象全攻略 (二) 实例化对象 使用对象成员
php面向对象全攻略 (三)特殊的引用“$this”的使用
php面向对象全攻略 (四)构造方法与析构方法
php面向对象全攻略 (五) 封装性
php面向对象全攻略 (六)__set() __get() __isset() __unset()的用法
php面向对象全攻略 (七) 继承性
php面向对象全攻略 (八)重载新的方法
php面向对象全攻略 (九)访问类型
php面向对象全攻略 (十) final static const关键字的使用
php面向对象全攻略 (十一)__toString()用法 克隆对象 __call处理调用错误
php面向对象全攻略 (十二) 抽象方法和抽象类
php面向对象全攻略 (十四) php5接口技术

PHP 中的 php 数学运算验证码实现代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-12   浏览: 103 ::
收藏到网摘: n/a

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