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

PHP
PHP-Javascript“返回上一页”无缓存问题
新手入门:PHP编程中“数组”的基础知识
新手入门:PHP编程中“字符串”的小常识
PHP教程.经验技巧
php完美结合mysql数据库记录分页显示
php设计模式介绍之伪对象模式
PHP程序不实用大型系统的九大原因
PHP程序开发中的中文编码问题
PHP:避免重复提交和检查数据来路
动态网页技术PHP中错误处理的一些方法
单元测试在每个层上对PHP代码进行检查
学习动态网页制作PHP技术的正则表达式
PHP 5.0对象模型深度探索之起步
成为PHP高手 学会懒惰地用PHP编程
实例讲解基于DB2及PHP的应用系统跨平台迁移
如何书写安全的PHP代码
PHP教程:Ajax进行Web开发
Flash+PHP+Mysql简单留言本制作实例教程
理解PHP中的MVC框架编程
使用PHP与XML进行网站编程

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


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