12
votes
Can you save a variable in session?
And for those needing this for Craft 3...
Set (previously called 'add'):
Craft::$app->getSession()->set($key, $value);
Get
Craft::$app->getSession()->get($key);
12
votes
Accepted
Can you save a variable in session?
Craft's HttpSessionService is a thin wrapper for PHP's global $_SESSION variable.
You can use:
craft()->httpSession->add($key, $value)
and:
craft()->httpSession->get($key)
to save and ...
10
votes
Accepted
Getting logged out of admin area when refreshing site
The problem was actually Chrome using iOS emulation when viewing the site, thus spoofing the UA string into something different than the usual Chrome UA string when logging into the admin area.
...
7
votes
How can I store Craft sessions in the database?
To store sessions in the DB in Craft 3, you can merge the following into your config/app.php file:
return [
'components' => [
'session' => [
'class' => yii\web\...
5
votes
Accepted
Limit number of sessions per user
This can be done with a plugin.
The plugin would create an 'activity' table that saves the current user's ID and a timestamp of their last known activity.
You can insert the first row by listening ...
5
votes
Accepted
__PHP_Incomplete_Class retrieved from session
Save yourself the headache and don't store an entire Model in session. More to the point, don't store any complex objects in session.
It's much better practice to store just the information you need ...
5
votes
Using PHP Redis as Session Store
This may help:
Add the following to the general.php
'cacheMethod' => 'redis',
'overridePhpSessionLocation' => 'tcp://localhost:6379'
Add the following to the index.php
ini_set('session....
5
votes
Keep on getting logged out when deploying using Capistano
The problem is that the basepath changes each time you deploy (Capistrano uses versioned release directories). The basepath is used to generate a session key, which is invalidated each time a new ...
4
votes
Accepted
Is it possible to override the default session notices?
If you place a PHP file in craft/translations named after a locale ID and returning an array, those values will be used instead.
craft/translations/en.php
<?php
return array(
'Logged in.' =&...
4
votes
Limit number of sessions per user
Im on Craft v3.4.25, and I've added the following event handler in my module to handle this. There are two scenarios here: either you want the new login to invalidate any existing session, or you want ...
4
votes
Accepted
How can I disable/decrease the number of calls to getAuthTimeout?
Unfortunately, the AJAX call to getAuthTimeout is hard coded to fire every 60 seconds. In other words, if you're looking for a config value, there isn't one – might be worth a feature request?
For a ...
4
votes
Implementing SimpleSAML PHP in Craft plugin
We managed to circumvent the issue by using the sql session storage SimpleSAML PHP also provides. Not a real solution but a viable workaround.
3
votes
Accepted
Using the CP login session expiration for the frontend
What you want is this:
// Returns how many seconds are left in the current user session.
// (int) The seconds left in the session, or -1 if their session
// will expire when their HTTP session ends.
...
3
votes
Unable to login on multi-node install
As of Craft 2.3, overridePhpSessionLocation is set to false by default. Meaning that wherever Apache (or whatever web server you're using) and PHP is configured to save the PHP session files, that's ...
3
votes
Accepted
Getting 'signed out' after exactly 1 minute of inactivity
This was related to the place that the sessions were being stored with my hosting company (Vidahost). Using the Utils section in Craft2, we managed to track it down to being stored with memcache. This ...
3
votes
Getting session Id in twig template
Old post, I know, but we've recently published a plugin to achieve this for a similar problem we were facing.
SessionExposer plugin can help you achieve this. To get the Session ID in the template ...
3
votes
CometChat integration
The Matt's answer was pretty complete and worked almost, but for the benefit of others, there is the changes required files to make it works:
in integration.php
find
define('SET_SESSION_NAME','');
...
3
votes
Accepted
What's the difference between CraftSessionId and a normal PHP session?
They are one in the same.
Craft simply renames it to "CraftSessionId" rather than the default "PHPSESSIONID".
The only additional things we do to it is set the "httpOnly" flag so that JavaScript can'...
3
votes
Accepted
How should I set an arbitrary session value in a plugin?
Try this:
$cookie = new HttpCookie('myCookieName', $data, [ 'expire' => time() + 3600 ]);
craft()->request->getCookies()->add( $cookie->name, $cookie );
Your code for reading the ...
3
votes
conditional does not run first in a file that is included using the extends feature for twig
The issue here is that extends isn't the same thing, neither conceptually or technically, as an include. When you extend a template with a layout template, the template is "decorated" by the layout. ...
3
votes
Accepted
Getting error "Your session has ended. Enter your password to log back in." every 30 seconds or so
Thanks for the troubleshooting help. It appears as though the issue was the cache and how Craft handles sessions.
For those who use a cache/CDN and are having issues with control panel session ...
3
votes
Accepted
How can I disable sessions for most of my Craft site?
I'm not sure there is going to be an easy way to do this in Craft 2, unfortunately.
In the upcoming Craft 3, we refactored the session logic so that a session will only get started on requests that ...
3
votes
Accepted
"Your session has ended" modal popping up constantly
Thanks to Brad's help, we discovered that the server was misconfigured with an incorrect value for session.cookie_domain. Fixing that resolved the issue in this case.
3
votes
Accepted
Is it safe to remove the craft/storage/runtime/sessions folder?
Docs
runtime/ – Pretty much everything in here is there for caching and logging purposes. Nothing that Craft couldn’t live without, if the folder happened to get deleted.
It is safe to delete it
3
votes
Accepted
Craft CMS 3 - Redis Sessions cause error
@boboldehampsink to the rescue: https://github.com/craftcms/cms/issues/3428
Update:
This article covers things really well.
https://nystudio107.com/blog/the-case-of-the-missing-php-session
php ini ...
3
votes
Accepted
Is it possible to change the amount of time a user stays logged in for?
Yes! The setting is called userSessionDuration, and you can set it in your general.php config file, on a per-environment basis.
Here's an explanation of the values that you can use for the setting—for ...
3
votes
Accepted
How can I store Craft sessions in the database?
In Craft 3.4, you can run the craft setup/php-session-table console command.
That will create a phpsessions table in the database with the proper schema for MySQL or PostgreSQL.
Then from your ...
3
votes
Accepted
Can I manage/change the default flash message text?
You could for example just edit the translation file with the specific user registered flash message.
You can read more about the static translation in the docs.
2
votes
Accepted
Does Craft have any default keys for flash messages?
Not particularly.
UserSessionService has setNotice() and setErrors(), which are just wrappers for Yii's setFlash() where we pass in an 'error' or 'notice' for the key.
Yii will take than and prepend ...
2
votes
Accepted
CometChat integration
So, the Comet Chat is not able to read the userid, even if I made a Craft plugin who writes UserID in $_SESSION['userid'].
To access Craft's session variables outside Craft, you need to pull the ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
session × 68users × 13
plugin-development × 9
control-panel × 8
login × 8
config × 7
cookies × 7
craft3 × 6
admin × 6
templating × 4
php × 4
current-user × 4
redis × 4
cache × 3
craft2 × 3
timeout × 3
user-profile × 2
security × 2
usermodel × 2
aws × 2
configuration × 2
entries × 1
plugin-craftcommerce × 1
localization × 1
database × 1