Search code examples
phpsessionopencartopencart-3

How to check if user is logged in opencart version 3.x


I'm using OpenCart 3.0.3.2

I want to check if the user is logged in or not in a file called test.php located outside opencart folder. for example, if the shop URL is http://www.example.com/shop then the PHP file to check is in http://www.example.com/test.php

I saw in this link someone is asking the same question before 2 years. check user is logged in to opencart or not

This was for version 2.x, and when I try it did not work. So how to do it in version 3.0.3.2?

I have tried different things with the session but nothing works.


Solution

  • Just tested both those answers combined. Works perfect. Check the following:

    1. Both OpenCart and test.php should be on the same domain and same server. Folder on the level higher than OpenCart root is OK, because we are working with PHP $_SESSION.
    2. Firstly user have to go to OpenCart and log in (or not) so the session will receive new data.

    Lets collect all that solutions together. Open from your OpenCart 3 root catalog/controller/common/header.php and find

    class ControllerCommonHeader extends Controller {
        public function index() {
    

    add below

    session_start();
    $_SESSION['opencart'] = $this->session->data;   
    

    Now go to your test.php and add

    <?php
    if(!empty($_SESSION['opencart']['customer_id'])){
      // User Loged in
    }
    ?>