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

PHP
需要发散思维学习PHP
php 图片上添加透明度渐变的效果
php 图片上传类代码
PHP 生成的XML以FLASH获取为乱码终极解决
PHP 远程文件管理,可以给表格排序,遍历目录,时间排序
PHP DataGrid 实现代码
MayFish PHP的MVC架构的开发框架
PHP 实现多服务器共享 SESSION 数据
PHP 开源框架22个简单简介
PHP 数组遍历顺序理解
PHPLog php 程序调试追踪工具
PHP 分页类(模仿google)-面试题目解答
火车头discuz6.1 完美采集的php接口文件
Discuz 6.0+ 批量注册用户名
使用php来实现网络服务
火车采集器 免费版使出收费版本功能实现原理
php程序之die调试法 快速解决错误
PHP 日常开发小技巧
PHP下通过系统信号量加锁方式获取递增序列ID
php 代码优化的42条建议 推荐

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-12   浏览: 102 ::
收藏到网摘: 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);
?>