CakePHP continue session outside cake - session

I'm trying to continue the CakePHP session outside the application.
The CakePHP session config:
Configure::write('Session', array(
'checkAgent' => false,
'defaults' => 'cake',
'timeout' => 10080, // 1 week,
'ini' => array(
'session.cookie_httponly' => 1,
)
));
cakephp_webroot/test_session.php:
<?php
session_name("CAKEPHP");
session_start();
var_dump($_SESSION);
?>
test session.php should output the cake session, but it is not working. I've verified the cookie CAKEPHP is present.

You are using the cake defaults for session handling, they are not compatible with the PHP defaults (available as php for the defaults option).
The cake configuration uses a custom save path and enforces cookie usage.
https://github.com/cakephp/.../Datasource/CakeSession.php#L600-L612
// ...
'cake' => array(
'cookie' => 'CAKEPHP',
'timeout' => 240,
'ini' => array(
'session.use_trans_sid' => 0,
'url_rewriter.tags' => '',
'session.serialize_handler' => 'php',
'session.use_cookies' => 1,
'session.cookie_path' => self::$path,
'session.save_path' => TMP . 'sessions',
'session.save_handler' => 'files'
)
),
// ...
So either configure your external scripts session usage the same as the cake defaults, or use the php defaults instead, and control things via your PHP ini configuration.
See also
Cookbook > Development > Sessions > Built-in Session handlers & configuration
http://php.net/manual/en/session.configuration.php

Related

How to configure cache for nodge eauth in Yii2

Now, I use default cache for all:
'cache' => [
'class' => 'yii\caching\FileCache',
],
And I use nodge eauth for registration users.
Default config nodge:
'components' => [
'eauth' => array(
'class' => 'nodge\eauth\EAuth',
'popup' => true, // Use the popup window instead of redirecting.
'cache' => false, // Cache component name or false to disable cache. Defaults to 'cache' on production environments.
'cacheExpire' => 0, // Cache lifetime. Defaults to 0 - means unlimited.
)
]
What should I specify to cache?
If you want to cache requests, your config should be
'eauth' => [
'class' => 'nodge\eauth\EAuth',
'popup' => false, // Use the popup window instead of redirecting.
'cache' => 'cache', // Cache component name or false to disable cache. Defaults to 'cache' on production environments.
'cacheExpire' => 0, // Cache lifetime. Defaults to 0 - means unlimited.]
But I can be wrong.

CakePHP 3 losing Auth Session

I am using Auth Session in CakePHP.
'Session' => [
'timeout' => 0,
'defaults' => 'php',
'ini' => [
'session.cookie_secure' => false,
'session.cookie_lifetime' => 0
]
],
Already configured this in tha app.php but still getting logged out after few minutes.
Although Session persists when closing Browser. So i think its just a time related problem.
I was having requirement to increase session timeout in cakphp 2.5
the following code worked for me in 2.5 you can give this a try
//FILE: config/core.php
Configure::write('Session', array(
'defaults' => 'cake',
'cookie' => 'my_app',
'timeout' => 500, // 8 hours + 20 min, user will be logged in till 8 hours.
)
);

Using and Configuring Zend Session and Zend Cache Memcached - Zend Framework 2.3

Actually, I'm using "standard" sessions manager config:
http://framework.zend.com/manual/current/en/modules/zend.session.manager.html
I want to use cache and save my session's data into server's cache (memcached) for improves performances and scalability.
I set php.ini like this (localhost memcached):
session.save_handler=memcached
session.save_path= "tcp://127.0.0.1"
and it show this error:
Warning: session_start(): Cannot find save handler 'memcached' - session startup failed in C:\Program Files (x86)\xampp\htdocs\Zend-application\vendor\zendframework\zendframework\library\Zend\Session\SessionManager.php on line 98
So, I don't understand how to configure my config/autoload/global.php and module/application/module.php. it's my first time that I want to implement memcached and caching in general. thanks, so much!
I tried to modify module/application/module.php like this:
---add session and cache ---
use Zend\Session\Config\SessionConfig;
use Zend\Session\Container;
use Zend\Cache\StorageFactory;
use Zend\Session\SaveHandler\Cache;
use Zend\Session\SessionManager;
use Zend\Session\Validator\HttpUserAgent;
use Zend\Session\Validator\RemoteAddr;
--- end session and cache ---
public function onBootstrap($e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$this->initSession(array(
'remember_me_seconds' => 180,
'use_cookies' => true,
'cookie_httponly' => true,
'validators' => array(
'Zend\Session\Validator\RemoteAddr',
'Zend\Session\Validator\HttpUserAgent',
'phpSaveHandler' => 'memcached',
'savePath' => 'tcp://127.0.0.1',
)
));
}
public function initSession($config)
{
$sessionConfig = new SessionConfig();
$sessionConfig->setOptions($config);
$sessionManager = new SessionManager($sessionConfig);
$sessionManager->getValidatorChain()
->attach(
'session.validate',
array(new HttpUserAgent(), 'isValid')
)
->attach(
'session.validate',
array(new RemoteAddr(), 'isValid')
);
$cache = StorageFactory::factory(array(
'adapter' => array(
'name' => 'memcached',
'options' => array(
'server' => '127.0.0.1',
),
)
));
$saveHandler = new Cache($cache);
$sessionManager->setSaveHandler($saveHandler);
$sessionManager->start();
Container::setDefaultManager($sessionManager);
}
but it shows this error:
Warning: ini_set() expects parameter 2 to be string, array given in C:\Program Files (x86)\xampp\htdocs\Zend-application\vendor\zendframework\zendframework\library\Zend\Session\Config\SessionConfig.php on line 88
Fatal error: Call to undefined method Zend\Stdlib\CallbackHandler::attach() in C:\Program Files (x86)\xampp\htdocs\Zend-application\module\Application\Module.php on line 68
this is my config/autoload/global.php
return array(
'db' => array(
'driver' => 'Pdo_Mysql',
'charset' => 'utf-8',
'dsn' => 'mysql:dbname=mydb;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
'session' => array(
'config' => array(
'class' => 'Zend\Session\Config\SessionConfig',
'options' => array(
'name' => 'zend-application',
),
),
'storage' => 'Zend\Session\Storage\SessionArrayStorage',
'validators' => array(
'Zend\Session\Validator\RemoteAddr',
'Zend\Session\Validator\HttpUserAgent',
),
),
);
Hoping it'll help someone, I resolved my issue. I'm working in Win7 enviroment and memcached doesn't work on it! I changed :
session.save_handler=memcached
session.save_path= "tcp://127.0.0.1"
to
session.save_handler=memcache
session.save_path= "tcp://127.0.0.1:11211"
I restored the "standard" session manager config and memcache works correctly. When I'll transfer the entire site to apache server, I'll change php.ini for using memcached.
http://framework.zend.com/manual/current/en/modules/zend.session.manager.html

CodeIgniter get_cookie / input->cookie not retrieving

I am setting a cookie like so:
$cookie = array(
'name' => 'test',
'value' => 'test',
'expire' => 86500,
'domain' => '.iwantaspeaker.com',
'path' => '/'
//'secure' => TRUE
);
$this->input->set_cookie($cookie);
var_dump($this->input->cookie('test', false));
Which returns bool(false)
I get absolutely no response. I have in the config:
$config['cookie_prefix'] = "iwas_";
And the cookie is stored as iwas_test, so I have also tried $this->input->cookie("iwas_test",true); to no avail.
I can also see that the cookie is set in chrome:
Furthermore, I have also tried using the cookie helper. What must I do to retrieve the cookie correctly? the URL is on a local network machine, so the domain is pointed to the local IP with an entry in my hosts file, if this makes any difference.
ah-ha! make sure you dont pass expire as a string
$cookie = array(
'name' => 'test',
'value' => 'test',
'expire' => 86500, <--
'domain' => 'www.iwantaspeaker.com',
'path' => '/',
'secure' => TRUE <-- will only be set on https
);
// $this->ci->db->insert("UserCookies", array("CookieUserEmail"=>$userEmail, "CookieRandom"=>$randomString));
$this->input->set_cookie($cookie);
var_dump($this->input->cookie('iwas_test', false));

cakephp session.cooke_lifetime doesn't work

I am using this configuration in core.php:
Configure::write('Session', array(
'defaults' => 'php'
));
Configure::write('Session.save', 'custom_session_handler');
/**
* The level of CakePHP security.
*/
Configure::write('Security.level', 'high');
And custom_session_handler.php (in app/config)
<?php
ini_set('session.cookie_lifetime', 0);
?>
When I close browser session should be destroyed/not remembered - but it doesn't work, when I close browser I am still logged in...
I am using Auth component, cakePHP 2.0, php 5.3.9 on WAMP server on my PC.
And moreover in my php.ini Session.cookie_lifetime is set to 0 by default
I found it - it should be:
Configure::write('Session', array(
'defaults' => 'php',
'cookieTimeout' => 0,
));

Resources