PHP之验证码类
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2016/6/20
* Time: 14:29
*/ Class captcha{
//验证码类
protected $str="xaaxqwe556232assd"; //随机数
protected $code; //验证码
protected $length=4; //验证码长度
protected $width=80; //验证码宽度
protected $height=30; //验证码高度
protected $img;//验证码生成 //随机数
public function getcode()
{
$len=strlen($this->str)-1;
for($i=0;$i<4;$i++)
{
$this->code.=$this->str[mt_rand(0,$len)];
} } //生成背景
public function codeBg()
{
//新建一个图像
$this->img=imagecreatetruecolor($this->width,$this->height);
$color=imagecolorallocate($this->img,rand(1,100),rand(1,100),rand(1,100)); //红,绿,蓝
//$back=imagecolorallocate($this->img,0,0,0);
imagefilledrectangle($this->img,0,0,$this->width,$this->height,$color); } //生成干扰元素 public function setLine()
{
//干扰点
for($i=0;$i<1000;$i++)
{
$color=imagecolorallocate($this->img,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($this->img,rand(1,99),rand(1,99),$color);
}
//
// //干扰线
for($i=0;$i<5;$i++)
{
$color=imagecolorallocate($this->img,rand(0,255),rand(0,255),rand(0,255));
imageline($this->img,rand(0,$this->width),rand(0,$this->height),rand(0,$this->width),rand(0,$this->height),$color);
}
//
// //干扰线 } //生成元素头
public function outHeader()
{
header("Content-type:image/png");
} //写入验证码 public function writeString()
{
$red = imagecolorallocate ( $this->img , 255 , 0 , 0 );
imagestring($this->img,5,rand(1,15),rand(1,15),$this->code(),$red);
//$font = 'arial.ttf' ;
//imagettftext ( $this->img , 20 , 0 , 10 , 20 , $red , $font , $this->code );
}
//shu public function png()
{
$this->outHeader();
$this->codeBg();
$this->getcode();
$this->setLine(); $this->writeString();
session_start();
$_SESSION['code']=$this->code();
imagepng($this->img);
}
public function code(){
return strtoupper($this->code);
} } $ce=new captcha();
$ce->png();
var_dump($_SESSION['code']);
测试页面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Examples</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
</head>
<?php session_start(); ?>
<body>
<img title="点击刷新" src="./captcha.class.php" onclick="this.src='captcha.class.php?'+Math.random();"/>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name='captcha' />
<input type="submit" name='submit' value="buton" />
</form>
</body>
</html> <?php
if($_POST['submit']){
if($_POST['captcha']!==$_SESSION['code'])
{
echo "登录失败";
}else{
echo "成功";
}
} ?>
PHP之验证码类的更多相关文章
- PHP-解析验证码类--学习笔记
1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1 定义变量 //随机因子 private $char ...
- THINKPHP源码学习--------验证码类
TP3.2验证码类的理解 今天在学习中用到了THINKPHP验证码,为了了解究竟,就开始阅读TP验证码的源码. 源码位置:./ThinkPHP/Library/Think/Verify.class.p ...
- ThinkPHP 3.2.3 加减乘法验证码类
ThinkPHP 3.2.3 自带的验证码类位于 /ThinkPHP/Library/Think/Verify.class.php,字体文件位于 /ThinkPHP/Library/Think/Ver ...
- 一个漂亮的php验证码类(分享)
直接上代码: 复制代码 代码如下: //验证码类class ValidateCode { private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRS ...
- 【个人使用.Net类库】(4)验证码类
验证码是现在管理系统常用的一种保护用户帐户信息的一种功能. 验证码可以有效防止某个黑客对某一个特定注册用户用特定程序暴力破解方式进行不断的登录尝试,虽然这可能是我们登录麻烦一点,但是对用户的密码安全来 ...
- PHP编写的图片验证码类文件分享方法
适用于自定义的验证码类! <?php/* * To change this license header, choose License Headers in Project Propertie ...
- laravel加入验证码类几种方法 && Laravel引入第三方库的方法
1,使用require , inlcude 的方法将验证码类文件包含进来,再进行new 2,将验证码类文件放于Http目录下面,也就是和控制器controller放在一个目录下面,在验证码类文件中加上 ...
- 一个好用的PHP验证码类
分享一个好用的php验证码类,包括调用示例. 说明: 如果不适用指定的字体,那么就用imagestring()函数,如果需要遇到指定的字体,就要用到imagettftext()函数.字体的位置在C盘下 ...
- 简单实用的PHP验证码类
一个简单实用的php验证码类,分享出来 ,供大家参考. 代码如下: <?php /** @ php 验证码类 @ http://www.jbxue.com */ Class code { var ...
随机推荐
- 抽取AWR数据
使用$ORACLE_HOME/rdbms/admin/awrextr.sql $ sqlplus '/as sysdba' SQL*Plus: Release Production on Fri No ...
- TNS-01189: The listener could not authenticate the user
查看监听时,发现监听状态异常,报TNS-01189: The listener could not authenticate the user错误 $ lsnrctl stat LSNRCTL - P ...
- ORA-16179: incremental changes to "log_archive_dest_1" not allowed with SPFILE
SQL> alter system set log_archive_dest_1='E:\arch ' scope=both; alter system set log_archive_dest ...
- EBS R12.2快速安装前没有配置Global Inventory报错
EBS R12.2快速安装前没有配置Global Inventory,导致验证时"file systems"这一项没有通过,被标记了"X": (本图其它两个验证 ...
- Java基础之在窗口中绘图——绘制直线和矩形(Sketcher 2 drawing lines and rectangles)
控制台程序. import javax.swing.JComponent; import java.util.*; import java.awt.*; import java.awt.geom.*; ...
- linux:磁盘的分割、检验、格式化与挂载
新增一颗磁碟: 1.对磁碟进行分割,以建立可用的partition 2.对该分割槽partition进行格式化(format),以建立系统可用的filesystem 3.若要仔细点,可对刚刚建立的fi ...
- Eclipse Ctrl+Tab Alt+/ 快捷键
原来Eclipse的Next Editor 快捷键是 Ctrl+Tab 但是后几个版本将这个快捷键改为Ctrl+F6 了 在Keys设置下面 搜索 NextEditor 将其设置回来即可 同样 原来C ...
- C#: PerformanceCounter的使用
在实际编程中,有的时候需要密切注意CPU, Memory的变化.这个时候需要用到PerformanceCounter这个类,注意需要using System.Diagnostics; 这里只是在con ...
- AS-demo09
,mainifast: <uses-permission android:name="android.permission.SET_WALLPAPER"/> , < ...
- sql语句中的 inner join 、 left join 、 right join、 full join 的区别
简单明了地说,连接分内连接和外链接 假设有A和B两张表 内连接:inner join 表示把AB表的记录相符都显示出来,把AB表不符合条件的都排除 外连接分三种,即左连接(LEFT OUTER J ...