Xác thực
Xác thực là quá trình xác định người dùng chính xác. PHP hỗ trợ ba loại xác thực.FormAuthenticate - Nó cho phép bạn xác thực người dùng dựa trên dữ liệu POST mẫu. Thông thường, đây là biểu mẫu đăng nhập mà người dùng nhập thông tin vào. Đây là phương pháp xác thực mặc định.
BasicAuthenticate - Nó cho phép bạn xác thực người dùng bằng cách sử dụng xác thực HTTP cơ bản.
DigestAuthenticate - Nó cho phép bạn xác thực người dùng bằng cách sử dụng xác thực Digest HTTP.
Ví dụ cho FormAuthentication
Thực hiện các thay đổi trong tệp config / routes.php như trong đoạn mã sau.
config / routes.php
<?php use Cake\Core\Plugin; use Cake\Routing\RouteBuilder; use Cake\Routing\Router; Router::defaultRouteClass('DashedRoute'); Router::scope('/', function (RouteBuilder $routes) { $routes->connect('/auth',['controller'=>'Authexs','action'=>'index']); $routes->connect('/login',['controller'=>'Authexs','action'=>'login']); $routes->connect('/logout',['controller'=>'Authexs','action'=>'logout']); $routes->fallbacks('DashedRoute'); }); Plugin::routes();Thay đổi mã của tệp AppController.php như được hiển thị trong chương trình sau.
src / Controller / AppController.php
<?php namespace App\Controller; use Cake\Controller\Controller; use Cake\Event\Event; use Cake\Controller\Component\AuthComponent; class AppController extends Controller{ public function initialize(){ parent::initialize(); $this->loadComponent('RequestHandler'); $this->loadComponent('Flash'); $this->loadComponent('Auth', [ 'authenticate' => [ 'Form' => [ 'fields' => ['username' => 'username', 'password' => 'password'] ] ], 'loginAction' => ['controller' => 'Authexs', 'action' => 'login'], 'loginRedirect' => ['controller' => 'Authexs', 'action' => 'index'], 'logoutRedirect' => ['controller' => 'Authexs', 'action' => 'login'] ]); $this->Auth->config('authenticate', [ AuthComponent::ALL => ['userModel' => 'users'], 'Form']); } public function beforeRender(Event $event){ if (!array_key_exists('_serialize', $this=>viewVars) && in_array($this->response=>type(), ['application/json', 'application/xml'])) { $this->set('_serialize', true); } } }Tạo tập tin AuthexsController.php tại src / Controller / AuthexsController.php . Sao chép mã sau trong tệp bộ điều khiển.
src / Controller / AuthexsController.php
<?php namespace App\Controller; use App\Controller\AppController; use Cake\ORM\TableRegistry; use Cake\Datasource\ConnectionManager; use Cake\Event\Event; use Cake\Auth\DefaultPasswordHasher; class AuthexsController extends AppController{ var $components = array('Auth'); public function index(){ } public function login(){ if($this->request->is('post')){ $user = $this->Auth->identify(); if($user){ $this->Auth->setUser($user); return $this->redirect($this->Auth->redirectUrl()); } else $this->Flash->error('Your username or password is incorrect.'); } } public function logout(){ return $this->redirect($this->Auth->logout()); } } ?>Tạo một thư mục Authexs tại src / Template và dưới thư mục đó tạo một tập tin View có tên là login.ctp . Sao chép mã sau vào tệp đó.
src / Mẫu / Authexs / login.ctp
<?php echo $this->Form->create(); echo $this->Form->input('username'); echo $this->Form->input('password'); echo $this->Form->button('Submit'); echo $this->Form->end(); ?>Tạo một tệp Xem khác có tên logout.ctp . Sao chép mã sau vào tệp đó.
src / Mẫu / Authexs / logout.ctp
You are successfully loggedout.
Tạo một tệp Xem khác được gọi là index.ctp . Sao chép mã sau vào tệp đó.src / Mẫu / Authexs / index.ctp
You are successfully logged in. <?php echo $this->Html->link('logout',["controller" => "Authexs","action" => "logout"]); ?>Thực hiện ví dụ trên bằng cách truy cập URL sau.
Phần mềm lập trình PHP chuyên nghiệp
http: // localhost: 85 /PHP / auth
Đầu ra
Vì việc xác thực đã được thực hiện nên khi bạn cố gắng truy cập vào URL trên, bạn sẽ được chuyển hướng đến trang đăng nhập như hình dưới đây.
![]() |
| phần mềm lập trình PHP chuyên nghiệp |
Sau khi cung cấp thông tin xác thực chính xác, bạn sẽ được đăng nhập và chuyển hướng đến màn hình như hình dưới đây.
Sau khi nhấp vào liên kết đăng xuất , bạn sẽ được chuyển hướng đến màn hình đăng nhập lần nữa.

Không có nhận xét nào:
Đăng nhận xét