≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > PHP笔记

php 验证码生成类入门实例

人气:808 时间:2018-09-22

这篇文章主要为大家详细介绍了php 验证码生成类入门实例,具有一定的参考价值,可以用来参考一下。

对一个php验证码生成类代码感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!

/**
 * php验证码生成类代码
 *
 * @param 
 * @arrange 512-笔记网: q1010.com
 **/
   class ValidationCode
   {
	//属性
	private $width;
	private $height;
	private $codeNum;
	   private  $image;
	private $disturbColorNum;  //干扰元素数目
	private  $checkCode;
	function __construct($width=80,$height=20,$codeNum=4)
	 {
	 $this->width=$width;
	 $this->height=$height;
	 $this->codeNum=$codeNum;
	 $number=floor($width*$height/15);
	 if($number>240-$codeNum)
	{
	  $this->disturbColorNum=240-$codeNum;
	 }else
	  {
	  $this->disturbColorNum=$number;
	  }
	  $this->checkCode=$this->createCheckcode();
	}
	function getCheckCode()
	{
		   return $this->checkCode;
	}
	private function createImage(){
		  $this->image=imagecreatetruecolor($this->width,$this->height);
	$backcolor=imagecolorallocate($this->image,rand(225,255),rand(225,255),rand(255,255));
	imagefill($this->image,0,0,$backcolor);
	$border=imagecolorallocate($this->image,0,0,0);
	imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$border);
	}
	private function setDisturbColor(){
	 for($i=0;$i<$this->disturbColorNum;$i++){
	  $color=imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));
	 imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color);
	 }
	 for($i=0;$i<10;$i++)
	 {
				  $color=imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));
	  imagearc($this->image,rand(-10,$this->width),rand(-10,$this->height),rand(30,300),rand(20,300),55,44,$color); 
	 }
	}
	  private function outputText($fontFace=""){
	for($i=0;$i<$this->codeNum;$i++)
	{
	 $fontcolor=imagecolorallocate($this->image,rand(0,128),rand(0,128),rand(0,128));
	if($fontFace=="")
   {
	 $fontsize=rand(3,5);
	 $x=floor($this->width/$this->codeNum)*$i+5;
	 $y=rand(0,$this->height-15);
	 imagechar($this->image,$fontsize,$x,$y,$this->checkCode{$i},$fontcolor);
	}
	else
   {
	 $fontsize=rand(12,16);
	 $x=floor(($this->width-8)/$this->codeNum)*$i+8;
	 $y=rand($fontsize,$this->height-8);
	 imagettftext($this->image,$fontsize,rand(-45,45),$x,$y,$fontcolor,$fontFace,$this->checkCode{$i});
	}
	}
   }
   private function createCheckCode(){
	$code="23456789abcdefghijkmnpqrstuvwrst";
	$str="";
	for($i=0;$i<$this->codeNum;$i++)
	{
	 $char=$code{rand(0,strlen($code)-1)};
	 $str.=$char;
	}
	return $str;
   }
   private function outputImage()
	{
	if(imagetypes()&IMG_GIF)
	 {
	   header("Content-Type:image/gif");
		imagepng($this->image);
	 }else if(imagetypes()&IMG_JPG)
	 {
		header("Content-Type:image/jpeg");
		imagepng($this->image);
	 }else if(imagetypes()&IMG_PNG)
	 {
		header("Content-Type:image/png");
	  imagepng($this->image);
	 }else if(imagetypes()&IMG_WBMP){
				 header("Content-Type:image/vnd.wap.wbmp");
	 imagepng($this->image);
	 }else
	 {
	  die("PHP不支持图片验证码");
	 }
	}
		//通过该方法向浏览器输出图像
	function  showImage($fontFace="")
	{
	 //创建图像背景
			$this->createImage();
	 //设置干扰元素
		   $this->setDisturbColor();
	 //向图像中随机画出文本
	 $this->outputText($fontFace);
	 //输出图像
	 $this->outputImage();
	}
	function __destruct()
	{
	 imagedestroy($this->image);
	}
   }
   function checklogin(){
		if(empty($_POST['name']))
				die( '用户名不能为空');
	if(empty($_POST['password']))
	 die("密码不能为空");
	if($_SESSION['code']!=$_POST['vertify'])
	 die("验证码输入不正确".$_SESSION['code']);
	$username=$_POST['name'];
	$password=md5($_POST['password']);
	//检查是否存在
		 conndb($username,$password);
   }
   function conndb($name="",$ps=""){
		$conn=mysql_connect('localhost','root','123456');
	   if(!$conn) die("数据库连接失败".mysql_error());
	 mysql_select_db('5kan',$conn) or die('选择数据库失败'.mysql_error());
  mysql_set_charset('utf8',$conn);
  $sql="select id from k_user where  username='{$name}' and password='{$ps}'";
  $result=mysql_query($sql) or die("SQL语句错误".mysql_error());
  if(mysql_num_rows($result)>0)  die("登录成功");
  else  die("用户名或者密码错误");
  mysql_close($conn);
   }
	session_start();
   if(!isset($_POST['randnum']))
   {
	 $code=new ValidationCode(120,20,4);
	 $code->showImage("comicbd.ttf");  //显示在页面
  $_SESSION['code']=$code->getCheckCode();//保存在服务器中
   }
   else
   {
	checklogin();
   }
/***   来自四海网(www.q1010.com)   ***/

本文来自:http://www.q1010.com/173/860-0.html

注:关于php 验证码生成类入门实例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:验证码

您可能感兴趣的文章

  • php 输出验证码的实现方法
  • php 简单图片验证码示例
  • php 英文、数字验证码的完整代码
  • PHP 高自定义性安全验证码的简单示例
  • php 中文验证码函数的简单示例
  • PHP+AJAX 实现简单验证码示例
上一篇:php 自定义urlencode,urldecode函数的简单示例
下一篇:PHP CSV文件导入和导出类解析
热门文章
  • PHP 写入WRITE编码为UTF8的文件示例
  • PHP 中文字符串截取函数示例:支持gb2312,gbk,big
  • PHP 简单留言板的制作示例
  • 解决Fatal error: Call to undefined function mb_convert_encoding() in错误问题
  • PHP语言基础(标记、注释、变量、数组、常量、函数)示例
  • php 生成迅雷链接的简单示例
  • php 获取短网址的实现方法
  • PHP 通用分页类的简单示例
  • PHP 使用文件方式导入导出整个MYSQL数据库的实现方法
  • php 获取MYSQL错误的简单示例
  • 最新文章
    • 解决PHP使用redis实现统计缓存MySQL压力的问题
    • php 简单的上传进度条的简单示例
    • php 给html中引用的js和css路径打上版本号的实现方法
    • php 实现计算年龄精准到年月日的实例
    • php+ajax无刷新分页的简单示例
    • 解决php+ajax无刷新上传图片的问题
    • 解决PHP生成HTML静态页面的问题
    • 解决PHP使用uniqid函数生成唯一ID的问题
    • 解决PHP防刷票的一些问题
    • 微信access_token的获取开发的实现方法

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。