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

PHP
PHP入门指导:如何学习PHP?
PHP环境配置IIS5.0+PHP5.23+MYSQL5+phpMyAdmin
PHP学习入门的一些基础知识(菜鸟必看)
详解用phpmyadmin建立MYSQL数据库的过程
PHP简单演示如何使用模板制作静态页面
PHP程序员都应该会用的五个工具
php+fastcgi遭遇No input file specified.错误
PHP开发者都应该知道10个项目
Apache设置PHP环境方法
安全问题:编写安全的PHP代码
PHP初学:发散思维学习PHP
Windows XP下PHP+MySQL环境搭建
PHP和IIS 7.0的FastCGI模块
httpd.conf设置一个IP两个域名的方法
PHP基础:认识PHP
隐蔽木马,插入到PHP文件中
PHP实例:避免重复提交和检查数据来路
php教程:php设计模式之前言
php教程:php设计模式之编程惯用法
php教程:php设计模式介绍之值对象模式

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


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