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);
?>

相关文章

php设计模式 Mediator (中介者模式)

复制代码 代码如下: <?php /** * 中介者模式 * * 用一个中介对象来封装一系列的对象交互,使各对象不需要显式地相互引用从而使其耦合松散,而且可以独立地改变它们之间的交...

php下实现折线图效果的代码

<?php   Class ImageReport{  var $X;//图片大小X轴  var $Y;//图...

PHP+ajax实现二级联动菜单功能示例

PHP+ajax实现二级联动菜单功能示例

本文实例讲述了PHP+ajax实现二级联动菜单功能。分享给大家供大家参考,具体如下: 如何实现二级联动 工作原理 二级联动在开发中是比较常见的一个技术点,它主要运用了JS的局部刷新技术a...

PHP sprintf()函数用例解析

复制代码 代码如下: <?php //sprintf()函数,返回值为格式化后的字符串 string sprintf ( string $format [, mixed $args...

递归删除一个节点以及该节点下的所有节点示例

有的时候删除信息的时候,需要把这条信息下的所有的都删除,这个时候就需要递归删除了。下面是我在做部门管理中的删除部门功能的时候写的一段递归删除一个部门以及它的所有子部门的一段代码。仅供大家...