I have been facing issue related to Session timeout using Zend Framework 3. Session expired within 5-10 min. I had used the default code for the session, which Zf3 skeleton provides in global.php as below.
// Session configuration.
'session_config' => [
'cookie_lifetime' => 60*60*1, // Session cookie will expire in 1 hour.
'gc_maxlifetime' => 60*60*1, // Store session data on server maximum for 1 hour.
],
// Session manager configuration.
'session_manager' =>
[
'validators' => [
RemoteAddr::class,
HttpUserAgent::class,
]
],
// Session storage configuration.
'session_storage' => [
'type' => SessionArrayStorage::class
],
After using above code still session expired within 5-10 minutes.I want session expired time more than 30 minutes.How to configure it in Zf3.
Please provide solution.
You have the correct settings for the session manager, but this is not enough for these session settings to be used as the default one.
My assumption is that you do not make this session manager your default one. In order to make it, you need to instantiate it as early as possible.
One solution would be to do this is in module Module.php
use Zend\Mvc\MvcEvent;
use Zend\Session\SessionManager;
class Module
{
//...
/**
* This method is called once the MVC bootstrapping is complete.
*/
public function onBootstrap(MvcEvent $event)
{
$application = $event->getApplication();
$serviceManager = $application->getServiceManager();
// The following line instantiates the SessionManager and automatically
// makes the SessionManager the 'default' one.
$sessionManager = $serviceManager->get(SessionManager::class);
}
}
Reference
EDIT: My 2nd assumption is that you use the global path for your sessions(eg /var/lib/php/sessions).
In Debian, there is a cron that may clear sessions according to your php.ini session settings(/etc/cron.d/php).
This cron uses your php.ini "gc_maxlifetime" and probably clears your sessions.
To find out where your sessions are saved, use session_save_path(). Check that directory for your sessions.
To overcome this, you should set "save_path" and this path should not be shared with others applications or scripts on your server(you do not want another script using the global gc settings or its own, deleting your sessions).
Add
'save_path' => '/path/to/app/data/sessions'
in your 'session_config' array.
Related
I need to show some values in all app, and sometimes I need to use these values between controllers.
I was trying to use Session, but I maybe it is not the answer to that, because if I am not wrong, Laravel store the session data temporarily in memory and only after everything has been executed, will it dump the session data into storage (file, cookie, redis, whatever).
So for example, I have a controller like that:
class GiftController extends Controller
{
public function dashboard(){
$token = (Session::has('token')) ? Session::get('token') : NULL;
if (is_null($token)){
return view('test.erro', ['message' => 'no session!']);
}else{
return view('test.ok', ['message' => $token]);
}
}
public function setsession(){
Session::put('token','xxxxxxxxxxxx');
return redirect('test/dashboard');
}
}
In this case, if I do an echo on Session::get('token') into the setsession(), is showing the value, but when it goes to the dashboard(), the session shows nothing.
What I am doing wrong?
or What is the better Idea to use instead off session?
I am using the Laravel Framework 5.8.37
I found the solution but I got it with the markskayff help.
He told me to Check my .env file, and he was right! It was a different SESSION_DRIVER value from config/session.php
In config/session.php was 'driver' => env ('SESSION_DRIVER', 'file')
Despite not having .env file but app.yaml since I am doing this project in GCP app engine, the session in this file was SESSION_DRIVER: cookie.
So I changed in config / session.php the line 'driver' => env ('SESSION_DRIVER', 'file') to 'driver' => env ('SESSION_DRIVER', 'cookie').
And now it is working!
I use Yii2 sessions for storing session data for guest users.
During sessions there are files generated in #runtime directory. These files are named with a database id (e.g. 234.pdf).
When a user selects something, the selection is saved in the db, stored in the session and a file is generated:
$model->save();
file_put_contents($model->id.".pdf", ...);
Yii::$app->session->set("model_id",$model->id);
When a session expires the sesseion related files should be removed. The database contents have to be kept for statistical reasons.
How do I get recognized when a session expires?
Yii does not have configurable session event handlers, in your case I would create own specific session handler extending PHP's built-in SessionHandler.
namespace app\components;
class CustomSessionHandler extends \SessionHandler
{
public function destroy($sessionId)
{
// here you could implement your custom requirements
return parent::destroy($sessionId);
}
}
and here is your configuration
'components' => [
'session' => [
'handler' => \app\components\CustomSessionHandler::class
],
...
];
I have problems to get cookies to work in cakephp 3.5.x.
in earlier versions I've used the Cookie component but this is now deprecated. Its unclear for me how to use this new middlewarestuff for reading and writing cookies.
The documentation is unclear for me. It shows me how to set up the cookie middleware but not how to handle creating cookies in a controller. Is there anyone who has handled cookies in 3.5.x?
The middleware only replaces the encryption part of the Cookie component (which basically is the only thing it did as of CakePHP 3.0 anyways), if required it automatically encrypts and decrypts the cookies that you've configured.
You do not use the middleware to read or write cookies, that is done via the request and response objects, which is the default since CakePHP 3.
Reading and writing cookies from within a controller action can be as simple as:
$rememberMe = $this->request->getCookie('remember_me');
$this->response = $this->response->withCookie('remember_me', [
'value' => 'yes',
'path' => '/',
'httpOnly' => true,
'secure' => false,
'expire' => strtotime('+1 year')
]);
See also
Cookbook > Controllers > Request & Response Objects > Request > Cookies
Cookbook > Controllers > Request & Response Objects > Response > Setting Cookies
Cookbook > Controllers > Request & Response Objects > Cookie Collections
My case using Cake 3.8, just in case someone is lost as myself:
In your beforeFilter load the component
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
//Load components, like Cookie
$this->loadComponent('Cookie', ['expires' => '30 day']);
}
If cake complains:
Argument 1 passed to App\Controller\PController::beforeFilter() must be an instance of App\Controller\Event, instance of Cake\Event\Event given
Add the following to the top of your class:
use Cake\Event\Event;
And then reading and writing Cookies in your Controller action is breeze:
//Read
$fooVal = $this->Cookie->read('foo');
//Write
$this->Cookie->write('foo', 'bar');
Im using Zend Framework 3 and the SessionManager and im trying to build a controller plugin / view helper to display confirm dialogues after validating some Data . The idea was simply to set a Session variable with everything the confirm dialogue needs, reading it by the view, and unsetting it. But even this simple cycle fails. The plugin basically does this when invoked by the controller:
$dataArray = [
'some_data' => 'data'
];
$this->sessionManager->getStorage()->confirmDialog = $dataArray;
in the layout.phtml i call my view Helper which does this:
public function __invoke() {
$data = $this->sessionManager->getStorage()->confirmDialog;
$this->sessionManager->getStorage()->clear('confirmDialog');
return $this->getDialog($data);
}
I do inject the sessionManager to both the plugin and the view helper. When not clearing the variable after receiving its data i get the changed data from the session variable and it gets updated by the Plugin as it should.But when clearing the variable after the first time reading it, its always empty.
Here my global.php setup:
'session_manager' => [
'validators' => [
RemoteAddr::class,
HttpUserAgent::class,
]
],
'session_storage' => [
'type' => SessionArrayStorage::class
]
Because the value is passed by reference, when you clear it, you clear the read information with it also.
As I mentioned in a comment, I suggest using the default falsh messenger plugin, but if you want to create your own plugin, here is a hint from that's source code, which shows you how you can achieve a default clear after read from session.
https://github.com/zendframework/zend-mvc-plugin-flashmessenger/blob/843654a029a19c38e0c3b2e940e59edec75c3e4f/src/FlashMessenger.php#L165
This setting is actually tells the session container to drop that information after '1 hop', ie. in case of a next request.
In Yii2,I have in my config/main.php
'components' => [
'user' => [
'identityClass' => 'common\models\User',
'authTimeout' => 43200,
'loginUrl' => null,
],
...
]
when i am trying to see my authTimeout variable in my whole system is ok and everything works fine except my session get expire before authTimeout.I am using access_token for login because my frontend is angular and also using mdmsoft/yii2-admin for RBAC.
And i am not getting,why i am logging out before my authTimeout?
Thanks
It is probably because globally, session.gc_maxlifetime is set to be lower than what you have set in your application. You can use echo ini_get("session.gc_maxlifetime"); to get the current value.
This answer discusses how to increase that within your application. Note, that some hosts tend to override the session timeout value set in php.ini, as discussed in this thread. Even if that is the case, this answer would help.
If this is not the case, then please provide more information about your script, host and php configuration. I will update the answer accordingly.