PHPをベースにユーザー登録・ログイン機能を実装する方法
このコースは、PHPとWebフロントエンド技術を使用して、Webサイトの登録とログイン入力ページの実装、PHPプログラミングの学習と実践などを行います。興味のある学生は参照できます。
この記事では、PHPをベースとしたユーザー登録・ログイン機能を紹介します。このプロジェクトは、1.フロントエンドページの作成、2.認証コードの作成、3.登録とログインの実装、4.機能改善の4つのパートに分かれています。詳細は以下をご覧ください。
検証コードの作成
1. 実験の導入
この実験では、オブジェクト指向の考え方を使用して検証コード クラスをカプセル化します。そして、登録およびログインインターフェイスに表示されます。この実験を学ぶことで、PHP の OOP の考え方、GD ライブラリの使用法、検証コードの生成について理解できるようになります。
1.1 関連する知識ポイント
PHP
GDライブラリ
OOPプログラミング
<?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; } }
//创建图像资源 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 フォントを使用して画像に書き込みます Text
//创建干扰线条(默认四条) 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); } }
- 画像セットピクセル:単一ピクセルを描画します
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; } }
session_start(); //开启session $captcha = new Captcha(); //实例化验证码类(可自定义参数) $captcha->show(); //调用输出
<p class="form-group"> <p class="col-sm-12"> <img src="admin/Captcha.php" alt="" id="codeimg" onclick="javascript:this.src = 'admin/Captcha.php?'+Math.random();"> <span>Click to Switch</span> </p> </p>
要約: 以上がこの記事の全内容です。皆さんの学習に役立つことを願っています。
関連おすすめ:
以上がPHPをベースにユーザー登録・ログイン機能を実装する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











PHP 8.4 では、いくつかの新機能、セキュリティの改善、パフォーマンスの改善が行われ、かなりの量の機能の非推奨と削除が行われています。 このガイドでは、Ubuntu、Debian、またはその派生版に PHP 8.4 をインストールする方法、または PHP 8.4 にアップグレードする方法について説明します。

ファイルのアップロードを行うには、フォーム ヘルパーを使用します。ここではファイルアップロードの例を示します。

CakePHP は、PHP 用のオープンソース フレームワークです。これは、アプリケーションの開発、展開、保守をより簡単にすることを目的としています。 CakePHP は、強力かつ理解しやすい MVC のようなアーキテクチャに基づいています。モデル、ビュー、コントローラー

Visual Studio Code (VS Code とも呼ばれる) は、すべての主要なオペレーティング システムで利用できる無料のソース コード エディター (統合開発環境 (IDE)) です。 多くのプログラミング言語の拡張機能の大規模なコレクションを備えた VS Code は、

CakePHP はオープンソースの MVC フレームワークです。これにより、アプリケーションの開発、展開、保守がはるかに簡単になります。 CakePHP には、最も一般的なタスクの過負荷を軽減するためのライブラリが多数あります。

Deepseekの公式Webサイトは現在、ユーザーにショッピングエクスペリエンスを提供するための複数の割引アクティビティを開始しています。新規ユーザーはサインアップして10ドルのクーポンを取得し、視聴者全員に15%の限定時間割引を享受します。友人は報酬を獲得することもできます。また、買い物時に贈り物を償還するためにポイントを蓄積することができます。イベントの締め切りは違う。
