1

I am facing some issues regarding clearing cookies in yii2. When i am calling a logout function on button click i am trying to perform following actions:

  1. Set authtoken, and its expiration value to null
  2. if Step got performed then clear session and cookies

but the problem is after setting the authtoken and its expiration value to null control is not going under if block (Where i am clearing session and cookies).

   public function actionLogout()
{
    $userId             =   \Yii::$app->user->identity->id;
    $restobj            =   new RestController();

    $this->token        =   NuLL;
    $expire             =   Null;
    $data               =   ['userId'=>$userId,'token'=>$this->token,'expire'=>$expire];
    $data               =   json_encode($data);
    $authtoken          =   $restobj->updateItem(\app\urls\urls::setauthtoken, $data);

     if($authtoken)
     {
            $session = new Session();
            $session->close();
            $session->destroy();
            $cookies    =   \Yii::$app->response->cookies;
            unset($cookies['user_cookies']);
            Yii::$app->user->logout();
            return $this->goHome();
     }
}

updateItem function is calling this authtoken function:

<?php
namespace app\actions\userloginactions;
use Yii;
use yii\rest\ActiveController;
use app\models\Authmaster;
use yii\base\Action;

class AuthtokenAction extends Action
{
//function used in rest api call for user token
public function run()
{       
        $data       =   Yii::$app->getRequest()->getBodyParams();
        $userId     =   $data['userId'];
        $token      =   $data['token'];
        $expire     =   $data['expire'];
        $result     =   Authmaster::setauthtoken($userId,$token,$expire);
        return true;
}
}

setauthtoken function in model called from AuthtokenAction

public static function setauthtoken($userId,$token,$expire)
{
    return  Authmaster::updateAll(['token'=>$token,'expire'=>$expire],['user_id'=>$userId]);
}

when i click logout button it successfully sets the authtoken and expiration to null but it directly displays true as a result of AuthtokenAction function and control doesn't goes under if block. that function call is creating some problem if i comment that and write cookies clearing block directly then cookies gets cleared without any problem.

GAMITG
  • 3,810
  • 7
  • 32
  • 51
Sunil Kumar
  • 76
  • 1
  • 13

3 Answers3

3

Please check following code to clear all cookies. It is working for me, hope will work for you too.

Yii::$app->cache->flush()
Shaig Khaligli
  • 4,955
  • 5
  • 22
  • 32
  • Cookies clearing is working fine using my code as well but only if i comment out that function call(using updateItem) but if i uncomment that call then the result of that function call is not assigned to $authtoken variable instead it directly prints true on screen which is returned from AuthtokenAction – Sunil Kumar Apr 03 '16 at 09:36
  • The question is about logging a single user out. This will trash the cache for all users https://www.yiiframework.com/doc/api/2.0/yii-caching-cache#flush()-detail – 111 May 24 '18 at 02:02
1

Please try to use following line

$cookies = Yii::$app->response->cookies;
$cookies->remove('user_cookies');

Can you try this one?

if (isset($_SERVER['HTTP_COOKIE'])) {
    $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
    foreach($cookies as $cookie) {
        $parts = explode('=', $cookie);
        $name = trim($parts[0]);
        setcookie($name, '', time()-1000);
        setcookie($name, '', time()-1000, '/');
    }
}
  • Check this thread http://stackoverflow.com/questions/2310558/how-to-delete-all-cookies-of-my-website-in-php – Nirav Patel Apr 02 '16 at 13:29
  • Thanks but i think the problem is that function called using updateItem directly returns the result skipping the remaining code after that call it should transfer the control back and assign the result to the $authtoken variable which is not happening in my case – Sunil Kumar Apr 02 '16 at 14:20
1

Hope this helps others...

    $cookies = Yii::$app->response->cookies;
    $cookies->remove('username');
    unset($cookies['username']);

Found in the following referenced link: http://www.bsourcecode.com/yiiframework2/cookies-handling-in-yii-framework2-0/