3

I have an application written with PhalconPHP. I used phalcon command line tools to build up things. I want to implement a "remember-me" option for users. However, if I understand correctly, PhalconPHP creates cookies with unique session ids and an encryption (encryption part, I like). So, whenever users session is gone, I lost access to the cookie. How can I fix this?

I actually do not destroy the session, I just use cmd + Q to quit my browser after I set session and cookie. I tried with encryption and without.

To be more clear : I do not get any errors. Just can't find the cookie back. I got "no cookie found" echoed after I close the browser and turn it on again.

As a code example, below is how I try to achieve this feature;

My 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;
});

Set the cookie in controller;

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

Get the cookie in controller;

if ($this->cookies->has('remember-me')) {
    $user_id = (string) $this->cookies->get('remember-me');
} else {
    echo "no cookie found";
    die();
}
Süha Boncukçu
  • 913
  • 1
  • 10
  • 29

1 Answers1

3

Syntax for cookie :

setcookie(name,value,expire,path,domain,secure,httponly);

About path parameter :

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

By default cookie get created with the current path until you change it to save cookie on any other path or '/'

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.

You can read more about path and cookies from this links :

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

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

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

Mr. Engineer
  • 3,522
  • 4
  • 17
  • 34
  • For me question is, why browser does not save cookie for next session if `path` is not set.. – yergo Feb 12 '16 at 09:03
  • It's not like that. You can access cookie but on the same page where you've created your cookie.As per docs if you dont set your path then it will only accessible for current file or directory. – Mr. Engineer Feb 12 '16 at 09:21