Skip to content

Instantly share code, notes, and snippets.

@harikt
Last active August 29, 2015 14:08
Show Gist options
  • Save harikt/204f546786cb84dd3ce8 to your computer and use it in GitHub Desktop.
Save harikt/204f546786cb84dd3ce8 to your computer and use it in GitHub Desktop.
How to create remember me session cookie in Symfony2
<?php
require __DIR__ . '/vendor/autoload.php';
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
use Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag;
$options = array(
'cookie_lifetime' => 123456,
'cookie_httponly' => true,
);
$session = new Session(new NativeSessionStorage($options), new NamespacedAttributeBag());
$session->start();
if ($session->get('name')) {
echo " World ! " . $session->get('name') . " <br /> ";
echo " Hello " . $session->get('next');
} else {
// set and get session attributes
$session->set('name', 'Drak');
$session->set('another', 'Boom');
$session->set('next', 'Next');
}
@harikt
Copy link
Author

harikt commented Nov 1, 2014

So session life time is for all the session names, not just for one session. I thought in ZF2 session they are only storing the session lifetime for specific session. But I am wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment