如何在 Phalcon 中管理 cookie PHP
How to manage cookies in Phalcon PHP
我有一个用 PhalconPHP 编写的应用程序。我使用 phalcon 命令行工具来构建东西。我想为用户实现一个 "remember-me" 选项。但是,如果我理解正确的话,PhalconPHP 会创建具有唯一会话 ID 和加密(我喜欢加密部分)的 cookie。因此,每当用户会话消失时,我就失去了对 cookie 的访问权限。我怎样才能解决这个问题?
其实我并没有销毁session,我只是在设置session和cookie后用cmd + Q退出我的浏览器。我尝试过加密和不加密。
更清楚一点:我没有收到任何错误。就是找不到饼干回来了。关闭浏览器并再次打开后,我收到 "no cookie found" 回显。
作为代码示例,下面是我尝试实现此功能的方法;
我的services.php
/**
* Start the session the first time some component request the session service
*/
$di->setShared('session', function () {
$session = new SessionAdapter();
//$session->setId('crowgadgets');
$session->start();
return $session;
});
/**
* Set crypt for cookie encryption
*/
$di->set('crypt', function () {
$crypt = new Crypt();
$crypt->setKey('-#1+%&/k5l6&olr$'); // Use your own key!
return $crypt;
});
/**
* Set cookie universal
*/
$di->setShared('cookies', function () {
$cookies = new Cookies();
//$cookies->useEncryption(true);
return $cookies;
});
在控制器中设置cookie;
$this->cookies->set('remember-me', $auth['id'], time() + 15 * 86400);
在控制器中获取cookie;
if ($this->cookies->has('remember-me')) {
$user_id = (string) $this->cookies->get('remember-me');
} else {
echo "no cookie found";
die();
}
cookie 的语法:
setcookie(name,value,expire,path,domain,secure,httponly);
关于 path
参数:
Optional. Specifies the server path of the cookie. If set to "/"
, the
cookie will be available within the entire domain. If set to "/php/",
the cookie will only be available within the php directory and all
sub-directories of php. The default value is the current directory
that the cookie is being set in
默认情况下,cookie 使用当前路径创建,直到您将其更改为将 cookie 保存在任何其他路径或 '/'
The path on the server in which the cookie will be available on. If
set to '/', the cookie will be available within the entire domain. If
set to '/foo/', the cookie will only be available within the /foo/
directory and all sub-directories such as /foo/bar/ of domain. The
default value is the current directory that the cookie is being set
in.
您可以从此链接阅读有关 path
和 cookies
的更多信息:
http://php.net/manual/en/function.setcookie.php
我有一个用 PhalconPHP 编写的应用程序。我使用 phalcon 命令行工具来构建东西。我想为用户实现一个 "remember-me" 选项。但是,如果我理解正确的话,PhalconPHP 会创建具有唯一会话 ID 和加密(我喜欢加密部分)的 cookie。因此,每当用户会话消失时,我就失去了对 cookie 的访问权限。我怎样才能解决这个问题?
其实我并没有销毁session,我只是在设置session和cookie后用cmd + Q退出我的浏览器。我尝试过加密和不加密。
更清楚一点:我没有收到任何错误。就是找不到饼干回来了。关闭浏览器并再次打开后,我收到 "no cookie found" 回显。
作为代码示例,下面是我尝试实现此功能的方法;
我的services.php
/**
* Start the session the first time some component request the session service
*/
$di->setShared('session', function () {
$session = new SessionAdapter();
//$session->setId('crowgadgets');
$session->start();
return $session;
});
/**
* Set crypt for cookie encryption
*/
$di->set('crypt', function () {
$crypt = new Crypt();
$crypt->setKey('-#1+%&/k5l6&olr$'); // Use your own key!
return $crypt;
});
/**
* Set cookie universal
*/
$di->setShared('cookies', function () {
$cookies = new Cookies();
//$cookies->useEncryption(true);
return $cookies;
});
在控制器中设置cookie;
$this->cookies->set('remember-me', $auth['id'], time() + 15 * 86400);
在控制器中获取cookie;
if ($this->cookies->has('remember-me')) {
$user_id = (string) $this->cookies->get('remember-me');
} else {
echo "no cookie found";
die();
}
cookie 的语法:
setcookie(name,value,expire,path,domain,secure,httponly);
关于 path
参数:
Optional. Specifies the server path of the cookie. If set to
"/"
, the cookie will be available within the entire domain. If set to "/php/", the cookie will only be available within the php directory and all sub-directories of php. The default value is the current directory that the cookie is being set in
默认情况下,cookie 使用当前路径创建,直到您将其更改为将 cookie 保存在任何其他路径或 '/'
The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain. If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain. The default value is the current directory that the cookie is being set in.
您可以从此链接阅读有关 path
和 cookies
的更多信息:
http://php.net/manual/en/function.setcookie.php