Viết cookie
Phương thức write () được sử dụng để viết cookie. Sau đây là cú pháp của phương thức write ().![]() |
| Phần mềm lập trình PHP chuyên nghiệp |
Link đăng ký khóa học : Phần mềm lập trình PHP chuyên nghiệp.
Cake\Controller\Component\CookieComponent::write(mixed $key, mixed $value = null)
Phương thức write () sẽ lấy hai đối số, tên của biến cookie ($ key) và giá trị của biến cookie ($ value)
Thí dụ
$this->Cookie->write('name', 'Virat');
Chúng ta có thể truyền mảng tên, giá trị để viết nhiều cookie.
Đọc cookie
Phương thức read () được sử dụng để đọc cookie. Sau đây là cú pháp của phương thức read ().Cake\Controller\Component\CookieComponent::read(mixed $key = null)
Phương thức read () sẽ lấy một đối số, tên của biến cookie ($ key).
Thí dụ
echo $this->Cookie->read('name');
Kiểm tra Cookie
Phương thức check () được sử dụng để kiểm tra xem một khóa / đường dẫn có tồn tại hay không và có giá trị không null. Sau đây là cú pháp của phương thức check () .Cake\Controller\Component\CookieComponent::check($key)
Thí dụ
echo $this->Cookie->check(‘name’);
Xóa cookie
Phương thức delete () được sử dụng để xóa cookie. Sau đây là cú pháp của phương thức delete ().Cake\Controller\Component\CookieComponent::delete(mixed $key)
Phương thức delete () sẽ lấy một đối số, tên của biến cookie ($ key) để xóa.
$this->Cookie->delete('name');
Thực hiện các thay đổi trong tệp config / routes.php như được hiển thị trong chương trình 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('cookie/write',['controller'=>'Cookies','action'=>'write_cookie']); $routes->connect('cookie/read',['controller'=>'Cookies','action'=>'read_cookie']); $routes->connect('cookie/check',['controller'=>'Cookies','action'=>'check_cookie']); $routes->connect('cookie/delete',['controller'=>'Cookies','action'=>'delete_cookie']); $routes->fallbacks('DashedRoute'); }); Plugin::routes();Tạo một tập tin CookiesController.php tại src / Controller / CookiesController.php . Sao chép mã sau trong tệp bộ điều khiển.
src / Controller / Cookies / CookiesController.php
<?php namespace App\Controller; use App\Controller\AppController; use Cake\Controller\Component\CookieComponent; class CookiesController extends AppController{ public $components = array('Cookie'); public function writeCookie(){ $this->Cookie->write('name', 'Virat'); } public function readCookie(){ $cookie_val = $this->Cookie->read('name'); $this->set('cookie_val',$cookie_val); } public function checkCookie(){ $isPresent = $this->Cookie->check('name'); $this->set('isPresent',$isPresent); } public function deleteCookie(){ $this->Cookie->delete('name'); } } ?>Tạo một thư mục Cookies tại src / Template và dưới thư mục đó tạo một tập tin View có tên là write_cookie.ctp . Sao chép mã sau vào tệp đó.
src / Mẫu / Cookie / write_cookie.ctp
The cookie has been written.
Tạo một tệp Xem khác có tên là read_cookie.ctp trong cùng thư mục Cookie và sao chép mã sau vào tệp đó.
src / Mẫu / Cookie / read_cookie.ctp
The value of the cookie is: <?php echo $cookie_val; ?>
Tạo một tệp Xem khác có tên là check_cookie.ctp trong cùng thư mục Cookie và sao chép mã sau vào tệp đó.
src / Mẫu / Cookie / check_cookie.ctp
<?php if($isPresent): ?> The cookie is present. <?php else: ?> The cookie isn't present. <?php endif; ?>
Tạo một tệp Xem khác có tên là delete_cookie.ctp trong cùng thư mục Cookie và sao chép mã sau vào tệp đó.
src / Mẫu / Cookie / delete_cookie.ctp
The cookie has been deleted.
Thực hiện ví dụ trên bằng cách truy cập URL sau - http: // localhost: 85 / PHP / cookie / write
Điều này sẽ giúp bạn ghi dữ liệu trong cookie.
Truy cập URL sau đây để đọc dữ liệu cookie - http: // localhost: 85 / PHP / cookie / đọc
Truy cập URL sau để kiểm tra dữ liệu cookie - http: // localhost: 85 / PHP / cookie / check
Truy cập URL sau để xóa dữ liệu cookie - http: // localhost: 85 / PHP / cookie / delete

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