zoukankan      html  css  js  c++  java
  • PHP验证码的制作教程

    自己过去自学了PHP绘画验证码的教程,现在就把这一部分笔记跟大家分享,希望可以帮到大家。

    顺带,我会在后面把我整理的一整套CSS3,PHP,MYSQL的开发的笔记打包放到百度云,有需要可以直接去百度云下载,这样以后你们开发就可以直接翻笔记不用百度搜那么麻烦了。

     笔记链接:http://pan.baidu.com/s/1qYdQdKK 密码:pvj2

    下面主要从理论+实践代码进行讲解,后面有代码实例。

    一、验证码的介绍
    验证码是为全自动区分计算机和人类的图灵测试的缩写。是一种区分用户是计算机和人的公共全自动程序。

    二、验证码应用场景?
    (都是为了区分人还是机器,屏蔽机器请求)
    a)登录、注册确定提交前,做人/机器校验;
    b)发布、回复信息前,做人/机器校验;
    c)疑似机器请求时,做人/机器校验;
    ………………

     

    三、验证码服务的核心技术分析
    实现步骤:
      1.生成底图;
      2.生成验证内容;
      3.生成验证码图片;
      4.校验验证内容;

    技术点:
      a)底图的实现,并且添加干扰元素;
        依赖PHP图片处理库GD
        http://php.net/gd
      b)生成验证内容
        简单的随机数生成,使用PHP函数mt_rand();
        随机数字+字母生成,需要ASCII码理论基础;
        随机中文生成,需要UTF-8编码理论基础;
      c)验证内容保存在服务端;
        需要PHP操作SESSION基础
      d)验证内容的校验
        需要前端Ajax基础;

    注意事项:
      a)依赖GD扩展
      b)输出图片前,必须提前输出图片header信息;
      c)该方法默认输出为黑色背景

    从理论部分,然后按步骤进行一步一步的写下面的代码:

    验证码的实例:(基本的验证码

    <?php
    	session_start();
    	// 先制作底图
    	$image = imagecreatetruecolor(100, 30);
    	$bgcolor = imagecolorallocate($image, 255, 255, 255);//生成底片颜色,默认为黑色
    	imagefill($image, 0, 0, $bgcolor);//x,y轴上的位置
    
    /*// 在地图上显示随机数字
    	for($i=0;$i<4;$i++){
    		$fontsize=6;
    		$fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    		$fontcontent=rand(0,9);//数字0~9
    
    		// 关键的部分 (注意事项:控制好字体大小与分布,避免字体重叠或显示不全)
    		$x=($i*100/4)+rand(5,10); //写在的坐标上
    		$y=rand(5,10);
    
    		imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
    	}
    */
    /*	//数字和字母验证码
    	for($i=0;$i<4;$i++){
    		$fontsize=6;
    		$fontcolor=imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120));
    		$data ='abcdefghigkmnpqrstuvwxy3456789';
    		$fontcontent=substr($data, rand(0,strlen($data)),1);
    
    		$x=($i*100/4+rand(5,10));
    		$y=rand(5,10);
    		imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
    	}*/
    	
    	$captch_code="";
    	//字母验证码
    	for($i=0;$i<4;$i++){
    		$fontsize=6;
    		$fontcolor=imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120));
    		$data ='abcdefghigkmnpqrstuvwxy';
    		$fontcontent=substr($data, rand(0,strlen($data)),1);
    		$captch_code.=$fontcontent;
    
    		$x=($i*100/4+rand(5,10));
    		$y=rand(5,10);
    		imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
    	}
    	$_SESSION['authcode']=$captch_code;
    
    	// 添加点的干扰素
    	for($i=0;$i<200;$i++){
    		$pointcolor = imagecolorallocate($image, rand(50,200), rand(50,200), rand(50,200));
    		imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);
    	}	
    
    // 添加线干扰
    	for($i=0;$i<3;$i++){
    		$linecolor=imagecolorallocate($image, rand(80,220), rand(80,220),rand(80,220));
    		imageline($image, rand(1,99),rand(1,29), rand(1,99),rand(1,29),$linecolor);
    	}
    
    	header('content-type:image/png');//输出png的图片
    	imagepng($image);//生成图片
    
    
    
    	// 销毁图片
    	imagedestroy($image);
    
    ?>
    

      

    关于PHP的验证代码部分后,就是配上前端的显示页面代码:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<title>登录</title>
    </head>
    <body>
    
    	<form action="reg.php" method="post">
    	<img id="captcha_img" src="verify.php?r=<?php echo rand();?>">
    	<a href="javascript:void(0);" onclick="document.getElementById('captcha_img').src='verify.php?+Math.random()';" title="换一个?">看不清?</a><br>
    		验证码:<input type="text" name="authcode">
    		<input type="submit" >
    	</form>
    </body>
    </html>
    

     上面是数字的验证码,下面的这部分PHP是图片验证码的页面图片验证码的显示

    <?php
    
    	session_start();
    	
    // 图片的验证码
    	$table=array(
    		"pic0"=>'狗',
    		"pic1"=>'猫',
    		"pic2"=>'鱼',
    		"pic3"=>'鸟'
    		);
    
    	$index=rand(0,3);
    
    	$value=$table['pic'.$index];
    	$_SESSION['authcode']=$value;
    
    	$filename=dirname(__FILE__).'\pic'.$index.'.jpg';//需要自己准备好图片!!
    $contents=file_get_contents($filename); header('content-type:image/jpg'); echo $contents; ?>

     

    下面这个是汉字的显示代码:

    汉字的验证码:
    <?php
    	session_start();
    	// 先制作底图
    	$image = imagecreatetruecolor(200, 60);
    	$bgcolor = imagecolorallocate($image, 255, 255, 255);//生成底片颜色,默认为黑色
    	imagefill($image, 0, 0, $bgcolor);//x,y轴上的位置
    
    	$fontface='msyh.ttf';
    	$str="刘恒春美女帅哥看这里来好样的我知道了是";
    
    	$strdb=str_split($str,3);//每三个长算一个汉字
    	header("content-type:text/html;charset='utf8'");
    	$captch_code="";
    	//中文验证码
    	for($i=0;$i<4;$i++){
    		$fontcolor=imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120));
    
    		$index=rand(0,count($strdb));
    		$cn=$strdb[$index];
    		$captch_code.=$cn;
    
    		imagettftext($image, mt_rand(20,24), mt_rand(-60,60), (40*$i+20), mt_rand(30,35),$fontcolor,$fontface,$cn);
    	}
    	$_SESSION['authcode']=$captch_code;
    
    	// 添加点的干扰素
    	for($i=0;$i<200;$i++){
    		$pointcolor = imagecolorallocate($image, rand(50,200), rand(50,200), rand(50,200));
    		imagesetpixel($image, rand(1,199), rand(1,59), $pointcolor);
    	}	
    
    // 添加线干扰
    	for($i=0;$i<3;$i++){
    		$linecolor=imagecolorallocate($image, rand(80,220), rand(80,220),rand(80,220));
    		imageline($image, rand(1,199),rand(1,59), rand(1,199),rand(1,59),$linecolor);
    	}
    
    	header('content-type:image/png');//输出png的图片
    	imagepng($image);//生成图片
    
    
    
    	// 销毁图片
    	imagedestroy($image);
    	
    ?>
    

      

    综上的全部就是验证码代码的总结了,如果有需要可以到我下面的百度云里面下载对应的笔记。

    笔记链接:http://pan.baidu.com/s/1qYdQdKK 密码:pvj2

    我是一名热爱旅游,读书,学习的文艺程序员。 不甘于平庸,不甘于舒适,愿不断挑战经历不同的美好。 如果您和我有共同的爱好,本人热爱交友,可以联系我。 QQ:330168885
  • 相关阅读:
    centos7.4 系统安装指导
    win10下硬盘安装CentOS7
    CentOs7.X下配置FTP
    pyspider 安装使用过程的一些坑
    .Net Core 商城微服务项目系列(十三):搭建Log4net+ELK+Kafka日志框架
    .Net Core自动化部署系列(二):使用Jenkins打造镜像发布流水线
    Kubernetes 系列(六):Kubernetes部署Prometheus监控
    Kubernetes 系列(五):Prometheus监控框架简介
    .Net Core 商城微服务项目系列(十二):使用k8s部署商城服务
    Kubernetes 系列(四):使用Traefik访问.net core api
  • 原文地址:https://www.cnblogs.com/leoyi330/p/7026647.html
Copyright © 2011-2022 走看看