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

PHP
PHP中的代码安全和SQL Injection防范
PHP数组实例总结及说明
一个php的图片水印的程序
PHP常见漏洞被攻击原因分析
PHP 5.2.8 紧急发布 修复5.2.7严重漏洞
编写安全 PHP 应用程序的七个习惯
PHP中常用的函数库和一些小技巧
PHP安全基础原则与方法
PHP函数速查表
PHP开发语言中的精华和技巧
测试在每个层上对PHP代码进行检查
不同文件构建PHP程序的正确方式
用phpinfo来实现PHP配置统计
更好的构造开发模板 五种常见的PHP设计模式
PHP的十个高级技巧
PHP对文本数据库的基本操作方法
967个函式列表 PHP常用语法速查表
实用:JAVA事件模式下PHP如何实现
针对配置文件 PHP最常用的ini函数
用PHP开发qmail邮件服务器管理系统

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


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