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接口多继承及tarits实现多继承效果的方法

本文实例讲述了PHP接口多继承及tarits实现多继承效果的方法。分享给大家供大家参考,具体如下: 接口多继承 在PHP的面向对象中,接口可以继承接口。PHP类只能继承一个父类(单继承)...

浅析is_writable的php实现

以下函数可用于替换php内置的is_writable函数复制代码 代码如下://可用于替换php内置的is_writable函数function isWritable($filename...

理解php Hash函数,增强密码安全

1.声明 密码学是一个复杂的话题,我也不是这方面的专家。许多高校和研究机构在这方面都有长期的研究。在这篇文章里,我希望尽量使用简单易懂的方式向你展示一种安全存储Web程序密码的方法。 2...

php 判断网页是否是utf8编码的方法

//判断编码复制代码 代码如下:$encode = mb_detect_encoding($q, array('GB2312','GBK','UTF-8'));echo $encode....

PHP与Java对比学习日期时间函数

废话少说先来看PHP中 date():格式化一个本地时间或者日期,当前时间 2016年5月13日 15:19:49 使用函数date(),输出当前是月份中的第几天,参数:String类型...