摘要:本項目發(fā)布在實驗樓,分為四部分內(nèi)容前端頁面制作,驗證碼制作,實現(xiàn)注冊登陸,功能完善。全部章節(jié)及代碼詳解可以在實驗樓中在線完成實現(xiàn)用戶注冊登錄功能驗證碼制作一實驗簡介本次實驗將會帶領大家使用面向?qū)ο蟮乃枷敕庋b一個驗證碼類。
項目簡介:本課程通過使用 PHP 及 Web 前端技術實現(xiàn)一個網(wǎng)站注冊登錄入口頁面,學習并實踐 PHP 編程,GD庫,MySQL 擴展,Bootstrap 響應式布局,Cookie/Session 及 Ajax 等知識點。
本項目發(fā)布在實驗樓,分為四部分內(nèi)容:1前端頁面制作,2驗證碼制作,3實現(xiàn)注冊登陸,4功能完善。本節(jié)實驗內(nèi)容為第二部分驗證碼制作部分。
全部章節(jié)及代碼詳解可以在實驗樓中在線完成:PHP 實現(xiàn)用戶注冊登錄功能
驗證碼制作 一、實驗簡介本次實驗將會帶領大家使用面向?qū)ο蟮乃枷敕庋b一個驗證碼類。并在注冊和登陸界面展示使用。通過本次實驗的學習,你將會領悟到 PHP 的 OOP 思想,以及 GD 庫的使用,驗證碼生成。
1.1 涉及到的知識點PHP
GD庫
OOP編程
1.2 開發(fā)工具sublime,一個方便快速的文本編輯器。點擊桌面左下角: 應用程序菜單/開發(fā)/sublime
1.3 效果圖 二、封裝驗證碼類 2.1 建立目錄以及準備字體在 web 目錄下建立一個 admin 目錄作為我們的后臺目錄,存放后臺代碼文件。在 admin 下建立一個 fonts 目錄,用于存放制作驗證碼所需字體。
在 admin 下新建一個 Captcha.php 文件,這就是我們需要編輯的驗證碼類文件。
當前目錄層次結構:
編輯 Captcha.php 文件:
添加該類的私有屬性和構造方法:
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接下來開始編寫具體的方法:
創(chuàng)建圖像資源句柄
//創(chuàng)建圖像資源 public function createImage(){ $this->img = imagecreate($this->width, $this->height); //創(chuàng)建圖像資源 imagecolorallocate($this->img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100)); //填充圖像背景(使用淺色) }用到的相關函數(shù)
imagecreate:新建一個基于調(diào)色板的圖像
imagecolorallocate:為一幅圖像分配顏色
mt_rand:生成更好的隨機數(shù)
創(chuàng)建驗證碼字符串并輸出到圖像
//創(chuàng)建驗證碼 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]); } }用到的相關函數(shù):
imagettftext:用 TrueType 字體向圖像寫入文本
創(chuàng)建干擾線條
//創(chuàng)建干擾線條(默認四條) 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); } }用到的相關函數(shù):
imageline:畫一條線段
創(chuàng)建干擾點
//創(chuàng)建干擾點 (默認一百個點) 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); } }使用的相關函數(shù):
imagesetpixel:畫一個單一像素
對外輸出圖像:
public function show() { $this->createImage(); $this->createCode(); if ($this->lineFlag) { //是否創(chuàng)建干擾線條 $this->createLines(); } if ($this->piexFlag) { //是否創(chuàng)建干擾點 $this->createPiex(); } header("Content-type:image/png"); //請求頁面的內(nèi)容是png格式的圖像 imagepng($this->img); //以png格式輸出圖像 imagedestroy($this->img); //清除圖像資源,釋放內(nèi)存 }用到的相關函數(shù):
imagepng:以 PNG 格式將圖像輸出到瀏覽器或文件
imagedestroy:銷毀一圖像
對外提供驗證碼:
public function getCode(){ return $this->code; }完整代碼如下:
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; } }以上就是驗證碼類的全部代碼。看起來確實挺簡單的,不過用的圖像處理函數(shù)比較多,上面相關的函數(shù)我也做了必要的鏈接和用途說明。這些函數(shù)也不用死記硬背,遇到不清楚的,隨時查閱 PHP 官方文檔,最重要的是還有中文文檔。
2.2 使用驗證碼既然已經(jīng)封裝完畢,那就可以開始使用了。這里為了方便,直接在 Captcha 類的下方調(diào)用該類:
session_start(); //開啟session $captcha = new Captcha(); //實例化驗證碼類(可自定義參數(shù)) $captcha->show(); //調(diào)用輸出三、前端展示后端已經(jīng)準備好了驗證碼,前端界面就可以展示了,修改 index.php 中的注冊與登陸表單的驗證碼部分:
Click to Switch
img 標簽添加了點擊事件的 js 代碼,這樣就可以實現(xiàn)點擊更換驗證碼的功能!
效果圖:
四、完善到目前為止,我們的驗證碼模塊基本就完成了。學習到這里,大家應該對面向?qū)ο缶幊逃辛诉M一步的理解。也領悟到了一絲 OOP 思想。OOP 的三大特征:封裝,繼承,多態(tài)。我們這里只用到了一點封裝的思想。大家可以繼續(xù)完善和改進這個驗證碼類,設計出更加完美的類。這個實驗也告訴我們,PHP 的函數(shù)很多,不要死記硬背,多看官方文檔。
本項目完整教程和代碼詳解可以在實驗樓查看。
更多PHP經(jīng)典項目:PHP全部-課程
文章版權歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/30356.html
摘要:本人這次的案例就是做一個注冊賬號,登錄賬號,登陸后才可以查看某些資源的功能,其實就是簡單的操作數(shù)據(jù)庫。登錄表單里客云登錄你需要登陸后,才可以獲取本站資源請輸入賬號請輸入密碼登陸沒有賬號立即注冊登錄處理頁面。 幾乎大部分的網(wǎng)站都有注冊賬號,登錄系統(tǒng),這是非常需要的一個模塊。本人這次的案例就是做一個注冊賬號,登錄賬號,登陸后才可以查看某些資源的功能,其實就是簡單的php操作數(shù)據(jù)庫。 首先講...
摘要:本節(jié)將實現(xiàn)文章評論與用戶關聯(lián)的功能。關系定義首先修改與表,增加字段增加全部回滾并重新執(zhí)行遷移添加用戶表與文章表評論表的一對多關系添加文章評論表與用戶表的多對一關系同時,評論表的字段增加。同時,我們還自定義了返回的錯誤信息。 本節(jié)將實現(xiàn)文章、評論與用戶關聯(lián)的功能。 關系定義 首先修改 posts 與 comments 表,增加 user_id 字段 /database/migratio...
摘要:模擬登錄新浪微博的核心,也是與模擬登錄最大的不同,密碼加密。已經(jīng)實現(xiàn)模擬新浪微博登錄的功能,之后不再更新。 參考資料: http://www.csuldw.com/2016/11/10/2016-11-10-simulate-sina-login/ http://blog.csdn.net/fly_leopard/article/details/51148904 http://www....
摘要:本項目是一個簡單的全棧項目,前端新手可以拿來練練手。項目實現(xiàn)了一些簡單的功能,后臺可以對圖書進行錄入錄出掃碼或手動,前臺顯示錄入的圖書,并且前臺注冊登錄后可以將書的訂單發(fā)給服務器,并存到服務器。 Vue-book 2.0 Github 地址:https://github.com/percy507/v... 【覺得不錯就來個 star 吧 ^_^】 說明(菜鳥請進,大神繞道 ~) 前端...
閱讀 1167·2021-08-12 13:24
閱讀 3016·2019-08-30 14:16
閱讀 3331·2019-08-30 13:01
閱讀 2095·2019-08-30 11:03
閱讀 2793·2019-08-28 17:53
閱讀 3107·2019-08-26 13:50
閱讀 2287·2019-08-26 12:00
閱讀 970·2019-08-26 10:38