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

PHP
Zend Framework 入门:快速上手
Zend Framework 入门:多国语言支持
Zend Framework 入门:错误处理
Zend Framework 入门:页面布局
PHP会话:session 时间设定使用入门
PHP实例:PHP创建动态图像
PHP正则表达式的几则使用技巧
ASP.NET比拼PHP,谁是速度之王?
ImageTTFText函数实现图像加文字水印
整合Discuz用户登陆代码
smarty笔记--模板参数
如何正确理解 PHP 的错误信息
Web技术进阶—PHP构建网站
C/S、B/S软件技术上的比较
apache with ssl安装
如何实现给定日期的若干天以后的日期(有点类似VB中的DateAdd)
如何在PHP中判断某个函数是否被支持
在Linux下安装PHP,APACHE,MYSQL,PERL的方法
Whois 的PHP代码
将数据库的内容读到二维数组并按指定列输出

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


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