This is opencart tips to check if Admin is logged in the front page of Opencart 2.3 and Opencart version 3. You should be in the same browser. Now go to any controller and insert the code to check if user_id is set in the session. For example: Go to catalog/controller/common/header.php

Inside the index() method add the following code:

if($this->session->data['user_id']){
 echo 'Admin is logged in';
}else{
 echo 'Admin is not logged in';
}

Another way initialize the user object and check with isLogged() method

$loggeduser = new Cart\User($this->registry);
if($loggeduser->isLogged()){
 echo "Admin is in";
}else{
 echo "Admin not in";
}

This is how we can use Opencart library global objects methods.

4 COMMENTS

  1. First way gives me:
    in front page:
    `Notice: Undefined index: user_id in C:wamp64wwwitwcatalogcontrollercommonheader.php on line 62Admin is not logged in`

    while trying to login to /admin:
    `( ! ) Parse error: syntax error, unexpected ‘$data’ (T_VARIABLE), expecting function (T_FUNCTION) in C:wamp64wwwitwadmincontrollercommonlogin.php on line 39`

    In second way, front is clean but /admin:
    `( ! ) Parse error: syntax error, unexpected ‘$data’ (T_VARIABLE), expecting function (T_FUNCTION) in C:wamp64wwwitwadmincontrollercommonlogin.php on line 39`

    https://ibb.co/fX99bF

  2. But what if you have logged as admin to the back, and as a customer to the front (i’ve do this all the time while development)? In this case method gerLogged will return cusomer data, not admin.

    • It will return Admin getLogged.

      To check the customer check with something like below:

      $loggeduser = new Cart\Customer($this->registry);
      if($loggeduser->isLogged()){
      echo “Customer is in”;
      }else{
      echo “Customer not in”;
      }

      Hope it helps.

      Thanks

LEAVE A REPLY

Please enter your comment!
Please enter your name here