Asked  4 Months ago    Answers:  5   Viewed   97 times

Here I go what am doing is I am using

 Yii::app()->SESSION['userid']

with no

  Yii::app()->session->open();

at login

  Yii::app()->session->destroy();

at logout

I wanna know if dont do the open and destroy session is it worthy . Does Yii do it internally.

One more strange thing I dont know whats happening. In the same browser for a session I can login for multiple users .. this should not happen so.Is it that i am not using the open and destroy session methods .

 public function actionLogout()
{
    Yii::app()->user->logout();
    Yii::app()->session->clear();
    $this->redirect(Yii::app()->controller->module->returnLogoutUrl);
}

Please let me know how do i figure this out

 Answers

95

For creating yii session

Yii::app()->session['userid'] = "value";

You can get value like this

$sleep = Yii::app()->session['userid'];

And unset session like

unset(Yii::app()->session['userid']); # Remove the session

In case of user signs out , you have to remove all the session.

Yii::app()->session->clear();

After this, you need to remove actual data from server

Yii::app()->session->destroy();
Thursday, July 29, 2021
 
nasty
answered 4 Months ago
22

While other topics here on SO offer a chincy hack solution, or to simply fetch it through Yii::$app->user->identity, I am going to show you how to do it properly. You can bend Yii2 to your will, by virtually extending everything!

I normally work in the "advanced" app, but I am going to just use the "basic" app as a reference for everyone. I assume those who are using the "advanced" app know the differences and understand namespaces.

Namespace Crash Course: In basic, you have namespace apppathto. This is where the file is located. For example, you have ContactForm.php located in the app root models directory, so it's namespace is appmodels. In advanced, you have multiple apps (frontend & backend), so the app root is different, thus you have frontend as one app root, and backend as another. The models directory in the advanced app is at "frontend/models" and it's namespace is frontendmodels. -- So anywhere you see "app" in the namespace, you can replace it with frontend or backend for the "advanced" template.


Solution

First, we need to modify our app's config file to override the default User component's class. I will leave out anything not related, so you keep your identityClass defined, I just am not showing it. We are simply adding to, not replacing, the config. Also, where I show frontend/something, the same can be done for backend/something. I just am not going to repeat both over and over again...

Basic (config/web.php) -or- Advanced (frontend/config/main.php)

'components' => [
    'user' => [
        'class' => 'appcomponentsUser', // extend User component
    ],
],

Remember: If you are using the advanced template, replace app with frontend or backend!

I also want to point out, that you will need to create the "components" directory if you haven't done so. This is where many people add their custom Classes. Whether it be your own helper class, or extending a base Yii component (such as extending yiiwebController).

Create a new file in your components directory named User.php. In that file, place the following code:

<?php
namespace appcomponents;

use Yii;

/**
 * Extended yiiwebUser
 *
 * This allows us to do "Yii::$app->user->something" by adding getters
 * like "public function getSomething()"
 *
 * So we can use variables and functions directly in `Yii::$app->user`
 */
class User extends yiiwebUser
{
    public function getUsername()
    {
        return Yii::$app->user->identity->username;
    }

    public function getName()
    {
        return Yii::$app->user->identity->name;
    }
}

Note: username and name must be valid column names in your database's user table!

So now we can use this in our controller, view, anywhere.. Let's test it out. In views/site/index.php, add this to the bottom of the page:

<?= Yii::$app->user->name ?>
<?= Yii::$app->user->username ?>

If all is good, and you have values in the user table for name and username, they should be printed on to the page :)

Wednesday, March 31, 2021
 
ramdemon
answered 9 Months ago
15

the php-memcached extension supports session locking

http://us3.php.net/memcached

http://us1.php.net/manual/en/memcached.sessions.php

the memcache and memcached extensions look syntactically similar so it may not be too much of a headache to give it a try. (memcached has a stable version 2.1.0 released 2012-08-07).


if you are set on using memcache 2.2.7 you will most likely have to implement the lock yourself by setting some "session_is_locked" variable in your session and then releasing/unsetting it when the script is done writing to the session. Then you'd always need to check if that variable is set before continuing with any scripts which write to the session.

Wednesday, March 31, 2021
 
penpen
answered 9 Months ago
27

create migration:

$this->createTable('session', [
    'id' => $this->char(40)->notNull(),
    'expire' => $this->integer(),
    'data' => $this->binary(),
    'user_id' => $this->integer()
]);

$this->addPrimaryKey('session_pk', 'session', 'id');

add this to config:

'components' => [
    'session' => [
        'class' => 'yiiwebDbSession',
        'writeCallback' => function($session){
            return [
                'user_id' => Yii::$app->user->id
            ];
        }
        // 'db' => 'mydb',  // the application component ID of the DB connection. Defaults to 'db'.
        // 'sessionTable' => 'my_session', // session table name. Defaults to 'session'.
    ],
Saturday, May 29, 2021
 
TheFrack
answered 7 Months ago
27

Session storage is only available when you are serving your page from a server - you'll find that it doesn't work if you open the HTML page locally because there is no session.

In the case of browsers that may not support session storage, you can test for the feature:

if (window.sessionStorage) {
    ...
Monday, November 8, 2021
 
Godfather
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share