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中数字、字符与对象判断函数用法实例

本文实例讲述了php中数字、字符与对象判断函数用法。分享给大家供大家参考。具体分析如下: 在php判断数字,字符,对象,数组等包括有参见 is_bool()、is_int()、is_in...

php ZipArchive实现多文件打包下载实例

实例代码: public function downLoad($dataUrl,$saveName) { $datalist = [ ROOT_PATH.'/...

PHP如何防止XSS攻击与XSS攻击原理的讲解

PHP如何防止XSS攻击与XSS攻击原理的讲解

XSS又称CSS,全称Cross SiteScript(跨站脚本攻击), XSS攻击类似于SQL注入攻击,是Web程序中常见的漏洞,XSS属于被动式且用于客户端的攻击方式,所以容易被忽略...

php循环检测目录是否存在并创建(循环创建目录)

循环创建目录方法 这个会生成image.gif目录 复制代码 代码如下: $filepath = "test/upload/2010/image.gif"; mk_dir($filepa...

Php部分常见问题总结第1/2页

若有出错地方或者你有更好的想法,欢迎跟贴. 在提问题前请先仔细查阅PHP手册,MYSQL手册  以及PHPINFO里面的设置 另外希望你读一下PHP编程标准 ...