PHP验证码,php验证码代码_PHP教程
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP验证码,php验证码代码_PHP教程,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3269字,纯文字阅读大概需要5分钟。
内容图文
PHP验证码,php验证码代码
设计一个验证码类,在需要的时候可以随时调用
验证码类,保存为ValidateCode.class.php
1 <?php 2 //验证码类 3 session_start(); 4 class ValidateCode { 5 private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//随机因子 6 private $code;//验证码 7 private $codelen = 4;//验证码长度 8 private $width = 130;//宽度 9 private $height = 50;//高度 10 private $img;//图形资源句柄 11 private $font;//指定的字体 12 private $fontsize = 20;//指定字体大小 13 private $fontcolor;//指定字体颜色 14 //构造方法初始化 15 public function __construct() { 16 $this->font = './latha.ttf';//注意字体路径要写对,否则显示不了图片 17 } 18 //生成随机码 19 private function createCode() { 20 $_len = strlen($this->charset)-1; 21 for ($i=0;$i<$this->codelen;$i++) { 22 $this->code .= $this->charset[mt_rand(0,$_len)]; 23 } 24 } 25 //生成背景 26 private function createBg() { 27 $this->img = imagecreatetruecolor($this->width, $this->height); 28 $color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255)); 29 imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color); 30 } 31 //生成文字 32 private function createFont() { 33 $_x = $this->width / $this->codelen; 34 for ($i=0;$i<$this->codelen;$i++) { 35 $this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156)); 36 imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height / 1.4,$this->fontcolor,$this->font,$this->code[$i]); 37 } 38 } 39 //生成线条、雪花 40 private function createLine() { 41 //线条 42 for ($i=0;$i<6;$i++) { 43 $color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156)); 44 imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color); 45 } 46 //雪花 47 for ($i=0;$i<100;$i++) { 48 $color = imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)); 49 imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color); 50 } 51 } 52 //输出 53 private function outPut() { 54 header('Content-type:image/png'); 55 imagepng($this->img); 56 imagedestroy($this->img); 57 } 58 //对外生成 59 public function doimg() { 60 $this->createBg(); 61 $this->createCode(); 62 $this->createLine(); 63 $this->createFont(); 64 $this->outPut(); 65 } 66 //获取验证码 67 public function getCode() { 68 return strtolower($this->code); 69 } 70 }
注意:第16行中,要修改字体的路径,否则字体图片无法显示
实现,保存为captcha.php
1 session_start(); 2 require './ValidateCode.class.php'; //先把类包含进来,实际路径根据实际情况进行修改。 3 $_vc = new ValidateCode(); //实例化一个对象 4 $_vc->doimg(); 5 $_SESSION['authnum_session'] = $_vc->getCode();//验证码保存到SESSION中
页面使用
<img title="点击刷新" src="./captcha.php" align="absbottom" onclick="this.src='captcha.php?'+Math.random();"></img>
转载自:一个漂亮的php验证码类(分享)
http://www.bkjia.com/PHPjc/973822.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/973822.htmlTechArticlePHP验证码,php验证码代码 设计一个验证码类,在需要的时候可以随时调用 验证码类,保存为ValidateCode.class.php 1 ? php 2 // 验证码类 3 session...
内容总结
以上是互联网集市为您收集整理的PHP验证码,php验证码代码_PHP教程全部内容,希望文章能够帮你解决PHP验证码,php验证码代码_PHP教程所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。