php高安全验证码生成程序
发布:smiling 来源: PHP粉丝网 添加日期:2014-08-23 14:24:31 浏览: 评论:0
本生成程序需要调用一些字体库,你可以调用你系统自带的一些字体,当然也可以像dedecms一样自己把字体放到一个目录,这样在服务器上也可以使用.
php高安全验证码生成程序实例代码如下:
- <?php
- public function captcha(){
- $font_dir = $_SERVER ["DOCUMENT_ROOT"] . "your_ttf_file.ttf"; // 字体库
- $img_w = 58; // 设置图片宽
- $img_h = 20; // 设置图片高
- $font_size = 11; // 字体大小
- $angle_l = -10; // 左偏角
- $angle_r = 10; // 右偏角
- $code_str = "ABCDEFGHJKLMNPQRSTUVWXYZ36";
- $word_len = 4; // 验证码位数
- $padding = 5; // 每两个文字之间间隔
- $margin = 2; // 左侧边距
- $base_line = 15; // 文字基线位置
- $base_line_offset = 2; // 基准线偏移量
- $pixel_num = 3; // 杂点数目基数
- $pixel_color= 8; // 杂点只有 $pixel_color 种颜色 总的杂点数为$pixel_num*$pixel_color
- $noise_font_size = 1; // 杂点字体大小
- $session_key= "my.xoyo_captcha"; //自定义session键名
- header("Cache-Control: no-cache, must-revalidate");
- header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
- header("Pragma: no-cache");
- header("Cache-control: private");
- header('Content-Type: image/png');
- session_start();
- $word = "";
- $code_str_len = strlen($code_str) - 1;
- for ($i = 0; $i < $word_len; $i++) {
- $word.= $code_str[rand(0, $code_str_len)];
- }
- $_SESSION [$session_key] = strtolower($word);
- $image = imagecreatetruecolor($img_w, $img_h);
- imagefilledrectangle($image, 0, 0, $img_w - 1, $img_h - 1, imagecolorallocate($image, mt_rand(235, 255), mt_rand(235, 255), mt_rand(235, 255)));
- //绘制杂点
- for($i = 0; $i < $pixel_color; $i++){
- $noise_color = imagecolorallocate( $image, mt_rand(150,225), mt_rand(150,225), mt_rand(150,225) );
- for($j = 0; $j < $pixel_num; $j++) {
- imagestring( $image, $noise_font_size, mt_rand(-10, $img_w), mt_rand(-10, $img_h), $code_str[mt_rand(0, $code_str_len)], $noise_color );
- }
- }
- //绘制文字
- for ($i = 0; $i < $word_len; ++$i) {
- $color = imagecolorallocate($image, mt_rand(0, 100), mt_rand(20, 120), mt_rand(50, 150));
- imagettftext($image, $font_size, mt_rand($angle_l, $angle_r), $margin, mt_rand($base_line-$base_line_offset, $base_line+$base_line_offset), $color, $font, mb_substr($word, $i, 1, 'utf-8'));
- $margin += (imagefontwidth($font_size) + $padding);
- }//开源代码phpfensi.com
- imagepng($image);
- imagedestroy($image);
- exit;
- }
- ?>
Tags: php高安全 php验证码
- 上一篇:php支持中英文的加密解密类代码
- 下一篇:实用的一个php验证码类
相关文章
- ·支持中文字母数字、自定义字体php验证码程序(2014-08-23)
- ·实用的一个php验证码类(2014-08-23)
- ·个人写的PHP验证码生成类分享(2021-04-07)
- ·php实现的Captcha验证码类实例(2021-04-13)
- ·一个经典的PHP验证码类分享(2021-04-26)
- ·php实现的验证码文件类实例(2021-05-28)
- ·PHP实现简单实用的验证码类(2021-06-14)

推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)