12306验证码太难,试着用php自己做一个验证码吧!
2016-01-07 14:32
531 查看
<span style="font-family: Verdana, 'Lantinghei SC', 'Hiragino Sans GB', 'Microsoft Yahei', Helvetica, arial, 宋体, sans-serif; line-height: 16.8px; background-color: rgb(255, 255, 255);">今年春运买票的朋友们碰上了12306的新验证码,各种吐槽,各种从一堆相似图片中选择答案。最后果然被网友们玩坏了……</span>
其实验证码很简单,简单说就是一个用xx编程语言处理图片的操作。
大致思路这样的: 定好一个验证码规则,画一些内容到图片上,然后把我们传达给用户的信息放在某个地方(session、db)保存起
来,最终展现给用户。
demo地址: http://demo.cntnn11.com/images/imgcode.php
下边是php代码,注意:请注意字体路径
<?php session_start(); error_reporting(E_ALL); /** * 一个简单的加乘验证码类 * @author z53 * @datetime 2016-01-07 */ class codeimg { public $width = 150; public $height = 35; public $num_1 = 1; public $num_2 = 10; public $f_file = './font/YaHei.Consolas.1.12.ttf'; public $f_size = 13; public $c_type = 1; //验证码类型:1加乘混合出现/2纯加法/3纯乘法。默认为1 public $imgSourc= ''; public $line = true; public $lineNum = 0; private $sess = ''; //存入session的值 private $dev = FALSE;//调试模式,也就是不输出header("content-type: image/png;");也不生成图片。测试的 function __construct() { $this->genImg(); } function genImg() { $this->imgSourc = $this->createImgSourc(); if(!$this->dev) { header("content-type: image/png;"); imagepng($this->imgSourc); imagedestroy($this->imgSourc); } } function createImgSourc() { $this->imgSourc = imagecreate($this->width, $this->height); $bgcolor = imagecolorallocate($this->imgSourc, 255, 255, 255); //是否生成干扰线 if($this->line) { $this->genLine(); } $text = $this->genStr(); $strlen = mb_strlen($text); $y = intval(($this->height-$this->f_size)); $x = intval(($this->height-$strlen)); $textColor = imagecolorallocate($this->imgSourc, rand(0, 125), rand(0, 125), rand(0, 125)); if( !function_exists('imagettftext')) { exit('imagettftext 不存在!'); } else { imagettftext($this->imgSourc, $this->f_size, 0, $x, $y, $textColor, $this->f_file, $text); } return $this->imgSourc; } /** * 干扰线线的生成 * 根据$this->lineNum决定生成多少条线,默认为随机生成5~12条 */ function genLine() { $lineNum = empty($this->lineNum) ? rand(5, 12) : $this->lineNum; for($i = 1; $i <= $lineNum; $i++ ) { $linecolor = imagecolorallocate($this->imgSourc, rand(0 , 255), rand(0, 255), rand(0, 255)); imageline($this->imgSourc, rand(1 , $this->width), rand(1, $this->height), rand(1, $this->width), rand(1, $this->height), $linecolor); } } /** * 生成图片里的内容 * @return string */ function genStr() { $operArr = array('加', '乘'); $operNum = $this->c_type === 2 ? 0 : ($this->c_type === 3 ? 1 : rand(0, 1)); $numArr[0] = rand(intval($this->num_1), intval($this->num_2)); $numArr[1] = rand(intval($this->num_1), intval($this->num_2)); if($operNum === 1) { $numArr[2] = intval($numArr[0])*intval($numArr[1]); } else { $numArr[2] = intval($numArr[0])+intval($numArr[1]); } $temp_num = rand(0, 2); $this->sess = $numArr[$temp_num]; $numArr[$temp_num] = '**'; $string = $numArr[0].' '.$operArr[$operNum].' '.$numArr[1].' = '.$numArr[2]; return $string; } function getValue() { return $this->sess; } } header("content-type: text/html; charset=utf-8"); $objimg = new codeimg(); $_SESSION['scode'] = $objimg->getValue(); ?>
相关文章推荐
- PHP中strcmp()和strcasecmp()函数字符串比较用法分析
- NTP服务器配置
- 如何获知PHP程序占用多少内存(复制)
- 菜单 PHP 开发中的外围资源性能分析(一)
- WordPress中调试缩略图的相关PHP函数使用解析
- PHP的pm、pm.max_requests、memory_limit
- tftp传文件
- Yii系列总结:yii 标签用法
- php 接口实例
- PHP 日期函数说明
- XHProf的安装和使用(PHP性能测试神器)
- vs 打印信息到Output
- Phpwind v8.7怎么修改默认编辑器字体方法?
- php 多重继承的替代方案trait
- PHP中substr函数字符串截取用法分析
- PHP中addcslashes与stripcslashes函数用法分析
- Yii 1.0数据库操作 查询、增加、更新、删除(事务处理)
- Jmeter中ftp测试下载默认路径及文件
- PHP 中 Orientation 属性判断上传图片是否需要旋转(转)
- PHP 自 5.2 到 5.6 中新增的功能详解