使用PHP怎么實現一個用戶注冊登錄功能?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
1.1 涉及到的知識點
PHP
GD庫
OOP編程
1.2 開發工具
sublime,一個方便快速的文本編輯器。點擊桌面左下角: 應用程序菜單/開發/sublime
二、封裝驗證碼類
2.1 建立目錄以及準備字體
在 web 目錄下建立一個 admin 目錄作為我們的后臺目錄,存放后臺代碼文件。在 admin 下建立一個 fonts 目錄,用于存放制作驗證碼所需字體。
在 admin 下新建一個 Captcha.php 文件,這就是我們需要編輯的驗證碼類文件。
當前目錄層次結構:
編輯 Captcha.php 文件:
<?php
/**
* Captcha class
*/
class Captcha
{
function __construct()
{
# code...
}
}添加該類的私有屬性和構造方法:
<?php
/**
* Captcha class
*/
class Captcha
{
private $codeNum; //驗證碼位數
private $width; //驗證碼圖片寬度
private $height; //驗證碼圖片高度
private $img; //圖像資源句柄
private $lineFlag; //是否生成干擾線條
private $piexFlag; //是否生成干擾點
private $fontSize; //字體大小
private $code; //驗證碼字符
private $string; //生成驗證碼的字符集
private $font; //字體
function __construct($codeNum = 4,$height = 50,$width = 150,$fontSize = 20,$lineFlag = true,$piexFlag = true)
{
$this->string = 'qwertyupmkjnhbgvfcdsxa123456789'; //去除一些相近的字符
$this->codeNum = $codeNum;
$this->height = $height;
$this->width = $width;
$this->lineFlag = $lineFlag;
$this->piexFlag = $piexFlag;
$this->font = dirname(__FILE__).'/fonts/consola.ttf';
$this->fontSize = $fontSize;
}
}字體文件可通過以下命令下載到 fonts 目錄:
$ wget http://labfile.oss.aliyuncs.com/courses/587/consola.ttf
接下來開始編寫具體的方法:
創建圖像資源句柄
//創建圖像資源
public function createImage(){
$this->img = imagecreate($this->width, $this->height); //創建圖像資源
imagecolorallocate($this->img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100)); //填充圖像背景(使用淺色)
}用到的相關函數
imagecreate:新建一個基于調色板的圖像
imagecolorallocate:為一幅圖像分配顏色
mt_rand:生成更好的隨機數
創建驗證碼字符串并輸出到圖像
//創建驗證碼
public function createCode(){
$strlen = strlen($this->string)-1;
for ($i=0; $i < $this->codeNum; $i++) {
$this->code .= $this->string[mt_rand(0,$strlen)]; //從字符集中隨機取出四個字符拼接
}
$_SESSION['code'] = $this->code; //加入 session 中
//計算每個字符間距
$diff = $this->width/$this->codeNum;
for ($i=0; $i < $this->codeNum; $i++) {
//為每個字符生成顏色(使用深色)
$txtColor = imagecolorallocate($this->img,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
//寫入圖像
imagettftext($this->img, $this->fontSize, mt_rand(-30,30), $diff*$i+mt_rand(3,8), mt_rand(20,$this->height-10), $txtColor, $this->font, $this->code[$i]);
}
}用到的相關函數
imagecreate:新建一個基于調色板的圖像
imagecolorallocate:為一幅圖像分配顏色
mt_rand:生成更好的隨機數
創建驗證碼字符串并輸出到圖像
//創建驗證碼
public function createCode(){
$strlen = strlen($this->string)-1;
for ($i=0; $i < $this->codeNum; $i++) {
$this->code .= $this->string[mt_rand(0,$strlen)]; //從字符集中隨機取出四個字符拼接
}
$_SESSION['code'] = $this->code; //加入 session 中
//計算每個字符間距
$diff = $this->width/$this->codeNum;
for ($i=0; $i < $this->codeNum; $i++) {
//為每個字符生成顏色(使用深色)
$txtColor = imagecolorallocate($this->img,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
//寫入圖像
imagettftext($this->img, $this->fontSize, mt_rand(-30,30), $diff*$i+mt_rand(3,8), mt_rand(20,$this->height-10), $txtColor, $this->font, $this->code[$i]);
}
}用到的相關函數:
imagettftext:用 TrueType 字體向圖像寫入文本
創建干擾線條
//創建干擾線條(默認四條)
public function createLines(){
for ($i=0; $i < 4; $i++) {
$color = imagecolorallocate($this->img,mt_rand(0,155),mt_rand(0,155),mt_rand(0,155)); //使用淺色
imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}
}用到的相關函數:
imageline:畫一條線段
創建干擾點
//創建干擾點 (默認一百個點)
public function createPiex(){
for ($i=0; $i < 100; $i++) {
$color = imagecolorallocate($this->img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagesetpixel($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}
}使用的相關函數:
imagesetpixel:畫一個單一像素
對外輸出圖像:
public function show()
{
$this->createImage();
$this->createCode();
if ($this->lineFlag) { //是否創建干擾線條
$this->createLines();
}
if ($this->piexFlag) { //是否創建干擾點
$this->createPiex();
}
header('Content-type:image/png'); //請求頁面的內容是png格式的圖像
imagepng($this->img); //以png格式輸出圖像
imagedestroy($this->img); //清除圖像資源,釋放內存
}用到的相關函數:
imagepng:以 PNG 格式將圖像輸出到瀏覽器或文件
imagedestroy:銷毀一圖像
對外提供驗證碼:
public function getCode(){
return $this->code;
}
完整代碼如下:
<?php
/**
* Captcha class
*/
class Captcha
{
private $codeNum;
private $width;
private $height;
private $img;
private $lineFlag;
private $piexFlag;
private $fontSize;
private $code;
private $string;
private $font;
function __construct($codeNum = 4,$height = 50,$width = 150,$fontSize = 20,$lineFlag = true,$piexFlag = true)
{
$this->string = 'qwertyupmkjnhbgvfcdsxa123456789';
$this->codeNum = $codeNum;
$this->height = $height;
$this->width = $width;
$this->lineFlag = $lineFlag;
$this->piexFlag = $piexFlag;
$this->font = dirname(__FILE__).'/fonts/consola.ttf';
$this->fontSize = $fontSize;
}
public function createImage(){
$this->img = imagecreate($this->width, $this->height);
imagecolorallocate($this->img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
}
public function createCode(){
$strlen = strlen($this->string)-1;
for ($i=0; $i < $this->codeNum; $i++) {
$this->code .= $this->string[mt_rand(0,$strlen)];
}
$_SESSION['code'] = $this->code;
$diff = $this->width/$this->codeNum;
for ($i=0; $i < $this->codeNum; $i++) {
$txtColor = imagecolorallocate($this->img,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
imagettftext($this->img, $this->fontSize, mt_rand(-30,30), $diff*$i+mt_rand(3,8), mt_rand(20,$this->height-10), $txtColor, $this->font, $this->code[$i]);
}
}
public function createLines(){
for ($i=0; $i < 4; $i++) {
$color = imagecolorallocate($this->img,mt_rand(0,155),mt_rand(0,155),mt_rand(0,155));
imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}
}
public function createPiexs(){
for ($i=0; $i < 100; $i++) {
$color = imagecolorallocate($this->img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagesetpixel($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}
}
public function show()
{
$this->createImage();
$this->createCode();
if ($this->lineFlag) {
$this->createLines();
}
if ($this->piexFlag) {
$this->createPiexs();
}
header('Content-type:image/png');
imagepng($this->img);
imagedestroy($this->img);
}
public function getCode(){
return $this->code;
}
}以上就是驗證碼類的全部代碼??雌饋泶_實挺簡單的,不過用的圖像處理函數比較多,上面相關的函數我也做了必要的鏈接和用途說明。這些函數也不用死記硬背,遇到不清楚的,隨時查閱 PHP 官方文檔,最重要的是還有中文文檔。
2.2 使用驗證碼
既然已經封裝完畢,那就可以開始使用了。這里為了方便,直接在 Captcha 類的下方調用該類:
session_start(); //開啟session $captcha = new Captcha(); //實例化驗證碼類(可自定義參數) $captcha->show(); //調用輸出
三、前端展示
后端已經準備好了驗證碼,前端界面就可以展示了,修改 index.php 中的注冊與登陸表單的驗證碼部分:
<div class="form-group"> <div class="col-sm-12"> <img src="admin/Captcha.php" id="codeimg" onclick="javascript:this.src = 'admin/Captcha.php?'+Math.random();"> <span>Click to Switch</span> </div> </div>
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。