Session là gì ?
Session là công cụ giúp lưu lại thông tin tạm thời của người dùng tại lần đăng nhập hiện tại trên server nhằm quản lý người dùng trong 1 phiên làm việc.Ví dụ:
Khi khởi tạo 1 session:
- Trong 1 lần bật trình duyệt và truy cập vào 1 trang, trang đó sẽ khởi tạo 1 session.
- Trong khi tương tác với website: Người dùng mua hàng và chưa thanh toán -> thông tin đó được lưu tạm thời trên session để quản lý người dùng.
- Khi người dùng đóng trình duyệt thì mọi thông tin lưu trên người dùng sẽ bị xóa.
Session trong phalcon
Đăng kí sử dụng và bắt đầu 1 session service /config/services.php
<?php
use Phalcon\Session\Adapter\Files as Session;
// Start the session the first time when some component request the session service
$di->setShared('session', function () {
$session = new Session();
$session->start();
return $session;
});
Set/Get giá trị cho session
<?php
use Phalcon\Session\Bag as SessionBag;
//set giá trị
$this->session->set("user-name", "Michael");
//kiểm tra xem biến có giá trị ko và lấy giá trị
if ($this->session->has("user-name")) {
// Retrieve its value
$name = $this->session->get("user-name");
}
$this->session->remove("user-name"); //hủy 1 giá trị trong session
<?php
use Phalcon\Session\Adapter\Files as Session;
// Start the session the first time when some component request the session service
public function logoutAction()
{
// Destroy the whole session
$this->session->destroy();
}
No comments:
Post a Comment
hocphalcon.blogspot.com