public function testSessionFiles() { $session = new Phalcon\Session\Adapter\Files(); $this->assertFalse($session->start()); $this->assertFalse($session->isStarted()); @session_start(); $session->set('some', 'value'); $this->assertEquals($session->get('some'), 'value'); $this->assertTrue($session->has('some')); $this->assertEquals($session->get('undefined', 'my-default'), 'my-default'); }
public function testSessionFiles() { $session = new Phalcon\Session\Adapter\Files(); $this->assertFalse($session->start()); $this->assertFalse($session->isStarted()); @session_start(); $session->set('some', 'value'); $this->assertEquals($session->get('some'), 'value'); $this->assertTrue($session->has('some')); $this->assertEquals($session->get('undefined', 'my-default'), 'my-default'); // Automatically deleted after reading $this->assertEquals($session->get('some', NULL, TRUE), 'value'); $this->assertFalse($session->has('some')); @session_destroy(); }
public function testSessionFilesWrite() { $session_path = __DIR__ . '/cache'; ini_set('session.save_handler', 'files'); ini_set('session.save_path', $session_path); ini_set('session.serialize_handler', 'php'); // Write $session = new Phalcon\Session\Adapter\Files(); $session->start(); @session_start(); // https://github.com/phalcon/cphalcon/issues/11129 if (version_compare(phpversion(), "5.4.0", "<")) { $this->assertEquals($session::SESSION_NONE, $session->status()); } else { $this->assertEquals($session::SESSION_ACTIVE, $session->status()); } $session->set('some', 'write-value'); $this->assertEquals($session->get('some'), 'write-value'); $this->assertTrue($session->has('some')); $session_id = $session->getId(); $this->assertNotEmpty($session_id); session_write_close(); unset($session); // Check session file $session_file = $session_path . '/sess_' . $session_id; $this->assertTrue(is_file($session_file)); $this->assertNotEmpty(@file_get_contents($session_file)); // Read $session = new Phalcon\Session\Adapter\Files(); $session->start(); @session_start(); $session->setId($session_id); $this->assertTrue($session->has('some')); $this->assertEquals($session->get('some'), 'write-value'); $session->remove('some'); $this->assertFalse($session->has('some')); @session_write_close(); unset($session); // Check session file $this->assertTrue(is_file($session_file)); $this->assertEmpty(@file_get_contents($session_file)); @unlink($session_file); }
public function testSessionFilesWrite() { $session_path = __DIR__ . '/cache'; ini_set('session.save_handler', 'files'); ini_set('session.save_path', $session_path); ini_set('session.serialize_handler', 'php'); // Write $session = new Phalcon\Session\Adapter\Files(); $session->start(); @session_start(); $session->set('some', 'write-value'); $this->assertEquals($session->get('some'), 'write-value'); $this->assertTrue($session->has('some')); $session_id = $session->getId(); $this->assertNotEmpty($session_id); session_write_close(); unset($session); // Check session file $session_file = $session_path . '/sess_' . $session_id; $this->assertTrue(is_file($session_file)); $this->assertNotEmpty(@file_get_contents($session_file)); // Read $session = new Phalcon\Session\Adapter\Files(); $session->start(); @session_start(); $session->setId($session_id); $this->assertTrue($session->has('some')); $this->assertEquals($session->get('some'), 'write-value'); $session->remove('some'); $this->assertFalse($session->has('some')); @session_write_close(); unset($session); // Check session file $this->assertTrue(is_file($session_file)); $this->assertEmpty(@file_get_contents($session_file)); @unlink($session_file); }
* and we will insert it into all of our controllers. * @var DefaultDI */ $di = new Phalcon\DI\FactoryDefault(); /** * Return array of the Collections, which define a group of routes, from * routes/collections. These will be mounted into the app itself later. */ $di->set('collections', function () { return include './Routes/RouteLoader.php'; }); // As soon as we request the session service, it will be started. $di->setShared('session', function () { $session = new \Phalcon\Session\Adapter\Files(); $session->start(); $session->set('language', 'id'); return $session; }); $di->set('modelsCache', function () { //Cache data for one day by default $frontCache = new \Phalcon\Cache\Frontend\Data(array('lifetime' => 3600)); //File cache settings $cache = new \Phalcon\Cache\Backend\File($frontCache, array('cacheDir' => __DIR__ . '/Cache/')); return $cache; }); /** * Database connection is created based in the parameters defined in the configuration file */ $di->set('db', function () use($config) { try { $eventsManager = new Phalcon\Events\Manager();
<?php $session = new Phalcon\Session\Adapter\Files(array('uniqueId' => 'my-private-app')); $session->start(); $session->set('var', 'some-value'); echo $session->get('var');