Search code examples
phpsessionopencartopencart2.xopencart2.3

access session data of opencart through php file


I am new to opencart. I have my basic php code at http://localhost/testphp

and my opencart is installed at http://localhost/opencart. What I want to do is that in testphp, I have a page in which I want to check that if any user is logged in or not at opencart.

If it is logged in then I want to perform x function

and It not logged in then want to perform y function

I have tried to logged in to opencart and tried to print_r($_SESSION) in testphp. It is returning blank. How can I perform this ? Please help me


Solution

  • If you are using localhost then you can try this. or same hosting account or cpanel

    Go to catalog/controller/common/header.php

    Before "public function index() {" Add this code

    class ControllerCommonHeader extends Controller {
        public function index() {    
                 session_start();
                 $_SESSION['opencart'] = $this->session->data;
    

    You Have to Print Array in http://localhost/testphp Code :

    <?php
    session_start();
    echo '<pre>';
    print_r($_SESSION);
    echo '</pre>';
    ?>
    

    Your Output Will be

    Array
    (
        [opencart] => Array
            (
                [language] => en-gb
                [currency] => USD
                [customer_id] => 2
                [shipping_address] => Array
                    (
                        [address_id] => 2
                        [firstname] => Prashant
                        [lastname] => Bhagat
                        [company] => 
                        [address_1] => Surat
                        [address_2] => 
                        [postcode] => 395003
                        [city] => Surat
                        [zone_id] => 1485
                        [zone] => Gujarat
                        [zone_code] => GU
                        [country_id] => 99
                        [country] => India
                        [iso_code_2] => IN
                        [iso_code_3] => IND
                        [address_format] => 
                        [custom_field] => 
                    )
    
            )
    
    )