[TOC]
自己寫的驗證碼是這幾個文件
Captch.class.php(驗證碼工具類)
home.html(前臺頁面,入口文件)
check.php(檢查處理文件)
# Captcha.class.php
~~~
<?php
class Captcha{
//碼值庫
private $charset =<<<"herdoc"
abcdefghkmnprstuvwxyzabcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ01234567890123456789
lol英雄如下
戰爭女神 希維爾 輪子媽
曙光女神 蕾歐娜
風暴女神 迦娜
皎月女神 黛安娜
朋友圈都是賣面膜
真的好煩
herdoc;
private $code; //驗證碼字符串
private $codelen = 4; //驗證碼長度
private $width = 150; //寬度
private $height = 50; //高度
private $img; //圖形資源句柄
private $font; //指定的字體
private $fontsize = 20; //指定字體大小
private $fontcolor; //指定字體顏色
//構造方法初始化
/**
* @param $codelen=4,$width=150,$height=40,$fontsize = 20,$font="Elephant.ttf"
* @access public
*/
public function __construct($codelen=4,$width=150,$height=40,$fontsize = 20,$font="./simkai.ttf") {
$this->charset = str_replace(" ", "x", $this->charset);
$this->charset = str_replace("\n", "j", $this->charset);
$this->charset = str_replace("\r", "d", $this->charset);
$this->charset = str_replace("\t", "z", $this->charset);
$this->codelen = $codelen;
$this->width = $width;
$this->height = $height;
$this->fontsize = $fontsize;
$this->font = $font;
}
/**
* 生成隨機碼,也就是碼值,保存到$this->code
* @access private
* @return void
*/
private function createCode() {
$_len = mb_strlen($this->charset,'utf-8')-1;
$this->code=""; //初始化碼值字符串
for($i=0;$i<$this->codelen;++$i){
$rand_index = mt_rand(0,$_len);
$this->code .= mb_substr($this->charset,$rand_index,1,'utf-8');
}
/*
for ($i=0;$i<$this->codelen;++$i) {
$this->code .= mb_substr($this->charset,mt_rand(0,$_len),1,'utf-8');
}
*/
}
/**
* 生成背景色
* @access private
* @return void
*/
private function createBg() {
$this->img = imagecreatetruecolor($this->width, $this->height);
$color = imagecolorallocate($this->img, mt_rand(170,250), mt_rand(170,255), mt_rand(170,255));
imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);
}
/**
* 生成文字到畫布
* @access private
* @return void
*/
private function createFont() {
$_x = $this->width / $this->codelen;
for ($i=0;$i<$this->codelen;++$i) {
$this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,160),mt_rand(0,160));;
imagettftext($this->img,$this->fontsize,mt_rand(-30,50),$_x*$i+mt_rand(1,5),$this->height / 1.4,$this->fontcolor,$this->font,mb_substr($this->code,$i,1,'utf-8'));
}
}
/**
* 生成線條、雪花
* @access private
* @return void
*/
private function createLine() {
for ($i=0;$i<6;++$i) {
$color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}
for ($i=0;$i<100;++$i) {
$color = imagecolorallocate($this->img,mt_rand(150,200),mt_rand(200,255),mt_rand(150,255));
imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
}
//點
for($k=0;$k<300;$k++){
$color=imagecolorallocate($this->img,mt_rand(0,200),mt_rand(0,200),mt_rand(0,200));
imagesetpixel($this->img,mt_rand(1,$this->width-2),mt_rand(1,$this->height-2),$color);
}
//圓弧
for($a=0;$a<20;$a++){
$color=imagecolorallocate($this->img,mt_rand(0,200),mt_rand(0,200),mt_rand(0,200));
imagearc($this->img,mt_rand(10,$this->width-10),mt_rand(4,$this->height-5),mt_rand(20,100),mt_rand(20,100),20,100,$color);
}
}
/**
* 輸出到網頁
* @access private
*/
private function outPut() {
ob_end_clean(); //清空前面的輸出
if(imagetypes()&IMG_GIF){
header("Content-Type:image/gif");
imagegif($this->img);
}elseif(imagetypes()&IMG_PNG){
header("Content-Type:image/png");
imagepng($this->img);
}elseif(imagetypes()&IMG_JPEG){
header("Content-Type:image/jpge");
imagejpeg($this->img,null,100);
}else{
die("error:no image in the php server");
}
imagedestroy($this->img);
}
/**
* 對外生成
* @access private
* @return 驗證碼
*/
public function generateCode() {
$this->createBg();
$this->createCode();
$this->createLine();
$this->createFont();
$this->outPut();
}
/**
* 獲取驗證碼的正確碼值
* @access public
* @return string 小寫的驗證碼碼值
*/
public function getCode() {
return strtolower($this->code);
}
}
$c = new Captcha();
$c->generateCode();
@session_start();
$_SESSION['captcha'] = $c->getCode();
~~~
# home.html
~~~
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>session 圖片驗證實例</title>
<style type="text/css">
#login p{
margin-top: 15px;
line-height: 20px;
font-size: 14px;
font-weight: bold;
}
#login img{
cursor:pointer;
}
form{
margin-left:20px;
}
</style>
</head>
<body>
<?php
@session_start();
header('Expires:-1');
header('Cache-Control:no-cache');
header('Pragma:no-cache');
?>
<form id="login" action="./check.php" method="post">
<p>此例為session驗證碼,不區分大小寫,點擊圖片更換</p>
<p>
<span>驗證碼:</span>
<input type="text" name="validate" value="" size=10>
<img title="點擊刷新" src="Captcha.class.php" onclick="this.src='Captcha.class.php?r='+Math.random()"></img>
</p>
<p>
<input type="submit">
</p>
</form>
~~~
# check.php
~~~
<?php
@session_start();
echo "<a href='./home.html'>返回驗證</a><br /><br />";
if(isset($_POST["validate"])){
$validate=mb_strtolower(trim($_POST["validate"]),'utf-8');
echo "您剛才輸入的是!<font color='blue'>".$_POST["validate"]."</font>!<br>";
if($validate != $_SESSION["captcha"]){
//判斷session值與用戶輸入的驗證碼是否一致;
echo "<font color=red>輸入有誤</font>";
echo "<br />答案是!<font color='blue'>".$_SESSION['captcha'],"</font>!<br />";
}else{
echo "<font color=green>通過驗證</font>";
echo "<br />答案是!<font color='blue'>".$_SESSION['captcha'],"</font>!<br />";
}
}
?>
~~~
- OAuth
- 簡介
- 步驟
- 單點登錄
- .user.ini
- 時間轉換為今天昨天前天幾天前
- 獲取ip接口
- 協程
- 概念
- yield-from && return-values
- 協程與阻塞的思考
- 中間件
- mysqli異步與php的協程
- 代碼片段
- pdo 執行的sql語句
- 二進制安全
- 捕捉異常中斷
- global
- 利用cookie模擬登陸
- 解析非正常json
- 簡單的對稱加密算法
- RSA 加密
- 過濾掉emoji表情
- 判斷遠程圖片是否存在
- 一分鐘限制請求100次
- 文件處理
- 多文件上傳
- 顯示所有文件
- 文件下載和上面顯示所有文件配合
- 文件的刪除,統計,存數組等
- 圖片處理
- 簡介
- 驗證碼
- 圖片等比縮放
- 批量添加水印
- beanstalkd
- 安裝
- 使用
- RabbitMQ
- 簡介
- debain安裝
- centos安裝
- 常用方法
- 入門
- 工作隊列
- 訂閱,發布
- 路由
- 主題
- 遠程調用RPC
- 消息中間件的選型
- .htaccess
- isset、empty、if區別以及0、‘’、null
- php各版本
- php7.2 不向后兼容的改動
- php中的各種坑
- php7改變
- php慢日志
- 郵件
- PHPMailer實現發郵件
- 驗證郵件地址真實性
- 文件下載
- FastCgi 與 PHP-fpm 之間的關系
- openssl 加解密
- 反射
- 鉤子方法
- 查找插件
- opcode
- opcache使用
- opcache優化
- 分布式一致性hash算法
- 概念
- 哈希算法好壞的四個定義
- php實現
- java實現
- 數組
- jwt
- jwt簡介
- 單點登錄
- phpize
- GeoIP擴展
- php無法獲得https網頁內容的解決方案
- homestead運行的腳本
- Unicode和Utf-8轉換
- php優化
- kafka
- fpm配置
- configure配置詳解