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中计算时间差有时候是件很麻烦的事!  不过我相信任何语言,只要撑握了其中规率就能找到办法,绝对是化腐朽为神奇的。  工作中经常要进行日期计算,下面总结几个工作中...

php中的一些数组排序方法分享

A.内部排序(直接加载到内存进行排序):包括交换式排序(冒泡和快速法)、选择式排序、插入式排序 B.外部排序(因数据量大,需借助外部存储进行排序):包括合并排序、直接合并排序 【冒泡排序...

php检测图片主要颜色的方法

本文实例讲述了php检测图片主要颜色的方法。分享给大家供大家参考。具体实现方法如下: $i = imagecreatefromjpeg("image.jpg"); for ($x=0...

php使用curl代理实现抓取数据的方法

本文实例讲述了php使用curl代理实现抓取数据的方法。分享给大家供大家参考,具体如下: <?php define ( 'IS_PROXY', true ); //是否...

PHP中数组的三种排序方法分享

一、冒泡排序法 说明:找到最大的数,排列到最后面,然后继续找 例: 复制代码 代码如下: $arr = array(3,5,-1,0,2); for($i=0;$i<count($...