php - 如何在 Phalcon PHP 中管理 cookie

标签 php session cookies phalcon

我有一个用 PhalconPHP 编写的应用程序。我使用 phalcon 命令行工具来构建东西。我想为用户实现“记住我”选项。但是,如果我理解正确的话,PhalconPHP 会创建具有唯一 session ID 和加密(我喜欢加密部分)的 cookie。因此,每当用户 session 消失时,我就失去了对 cookie 的访问权限。我怎样才能解决这个问题?

其实我并没有销毁session,我只是在设置session和cookie后用cmd + Q退出我的浏览器。我尝试过加密和不加密。

更清楚一点:我没有收到任何错误。就是找不到 cookies 回来了。关闭浏览器并再次打开后,我收到“未找到 cookie”的 echo 。

作为代码示例,下面是我尝试实现此功能的方法;

我的服务.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;
});

在 Controller 中设置cookie;

$this->cookies->set('remember-me', $auth['id'], time() + 15 * 86400);

在 Controller 中获取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);

关于路径参数:

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.

您可以从以下链接阅读更多关于 pathcookies 的信息:

http://php.net/manual/en/function.setcookie.php

http://www.w3schools.com/php/php_cookies.asp

http://www.tutorialspoint.com/php/php_cookies.htm

关于php - 如何在 Phalcon PHP 中管理 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35337645/

相关文章:

php - pdf 下载的正确 HTTP header

PHP 到 DOC - CSS 和图像未在文档文件中应用

java - 自定义 JSP 标记中的 session

javascript - 无法在 Heroku 中仅通过 https 访问 http 上的 cookie

PHP 自动缩小。有没有?

php - 表单输入值文本被截断

通过 Memcache 的 Django session : Cannot find session key manually

android - SSL HttpClient 连接中的 session 过期错误

google-chrome - 从命令行读取 Chrome cookie

angular - 使用 angular-cli 在通用渲染中使用 cookie