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

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

相关文章

php多数据库支持的应用程序设计第1/2页

所以我想在主从数据库设计上,应该将所有会话相关表进行特殊对待。即:所有的会话数据表都可以更新和查询,当一个用户访问站点的时候,即将此用户绑定到指定数据库,所有会话访问和查询操作都对此数据...

详解PHP中array_rand函数的使用方法

前言 array_rand() 函数从数组中随机选出一个或多个元素,并返回。从一个数组中随机取出n个值,用array_rand()可以轻易的实现,当面对大数组的时候,我们会担心他的效率、...

Ubuntu中支持PHP5与PHP7双版本的简单实现

前言 最近在编写一个工具的时候,使用了PHP命名空间特性,在命名空间中如果想引用常量、函数,需要PHP5.6以上的版本,但我阿里云 ECS 上安装的版本是PHP 5.5.9,由于 ECS...

php实现的简单日志写入函数

本文实例讲述了php实现的简单日志写入函数。分享给大家供大家参考。具体实现方法如下: function log( $logthis ){ file_put_contents('log...

详解WordPress中用于合成数组的wp_parse_args()函数

wp_parse_args() 函数是 WordPress 核心经常用到的函数,它的用途很多,但最主要用来给一个数组参数(args)绑定默认值。 因为 wp_parse_args() 函...