2

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.

focus.style
  • 6,612
  • 4
  • 26
  • 38
loay
  • 73
  • 5

1 Answers1

1

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
}
?>
focus.style
  • 6,612
  • 4
  • 26
  • 38
  • Still not working, i wrote inside the if statement in php file echo "user is loged in"; but it never appear which mean the Sesson is always empty. And as you mention, first i visit opencart to start the session then i went to test.php @focus.style – loay Jul 14 '20 at 07:11
  • 1. Insert this code in test.php just for test `'; print_r($_SESSION); echo ''; ?>` – focus.style Jul 14 '20 at 09:10
  • 2. Clear Ocmod cache. *system/storage/modification/catalog/controller/common/header.php*. If you can see that file file there - remove it (it will appear again, cached by system) – focus.style Jul 14 '20 at 09:13
  • Thank you very much brother now it works :)). After removing that cache file problem solved. – loay Jul 14 '20 at 09:25