php 数学运算验证码实现代码

yipeiwu_com5年前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);
?>

相关文章

UCenter中的一个可逆加密函数authcode函数代码

复制代码 代码如下: function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) { $ckey_...

PHP入门教程之自定义函数用法详解(创建,调用,变量,参数,返回值等)

本文实例讲述了PHP自定义函数用法。分享给大家供大家参考,具体如下: Demo1.php <?php //标准函数,内置函数 echo md5('123456'...

PHP中去掉字符串首尾空格的方法

第一种方法:通过php自带的函数 <?php /* trim 去除一个字符串两端空格, rtrim 是去除一个字符串右部空格, ltrim 是去除一个字符串左部空格。 */ ?&g...

asp和php下textarea提交大量数据发生丢失的解决方法

我用textarea提交大量的阿数据 我开始字段类型选的是mediumtext,数据有丢失 后来我改成了longtext,数据依然丢失, 而且发现和mediumtext提交到数据库中的数...

php数组中删除元素之重新索引的方法

如果要在某个数组中删除一个元素,可以直接用的unset,但今天看到的东西却让我大吃一惊 复制代码 代码如下: <?php $arr = array('a','b','c',...