Jump to content

How to check if any employee is logged in OUTSIDE prestashop?


harlandraka94
 Share

Recommended Posts

I have an app running in the same domain as my prestashop shop.

 

I would like to do something like a single sign-on: you log into prestashop, the app knows you are logged in and doesn't ask for login again.

 

I saw this code in another thread:

require_once(dirname(__FILE__).'/../config/config.inc.php');   // points to the correct file
require_once(dirname(__FILE__).'/../config/settings.inc.php'); // points to the correct file

$context = Context::getContext();
 
if (isset($context->employee) && $context->employee->id > 0) {
    $id_employee = $context->employee->id;
    echo "ID Employee: $id_employee";
} else {
    echo "No employee logged in.";
}

die();
 
It doesn't work, it outputs "No employee logged in.", no matter if I'm logged in or not (I'm an admin).
 
If I print_r the context object, employee comes blank, even if I'm logged in.
 
Is it possible to check if an employee is logged in outside prestashop? How?
 
Thank you
Edited by harlandraka94 (see edit history)
  • Like 1

Share this post


Link to post
Share on other sites

The best way is enter in the ecosystem of PrestaShop through a file, controller, module, etc, etc.

 

This is an example with a simple file in the root....

 

Create a file in the root directory of the shop:

cookie_access.php

Add this code inside the file:

<?php

require_once dirname(__FILE__).'/config/config.inc.php';
$cookie = new Cookie('psAdmin', '', (int)Configuration::get('PS_COOKIE_LIFETIME_BO'));

if (isset($cookie->id_employee) && $cookie->id_employee) {
    echo 'ID Employee: '.$cookie->id_employee;
} else {
    echo 'No employee logged in.';
}

Now you can verify if an employee is logged in the Back Office by calling this file:

http://domain.com/cookie_access.php

Hope this help you!

  • Like 3

Share this post


Link to post
Share on other sites

The best way is enter in the ecosystem of PrestaShop through a file, controller, module, etc, etc.

 

This is an example with a simple file in the root....

 

Create a file in the root directory of the shop:

cookie_access.php

Add this code inside the file:

<?php

require_once dirname(__FILE__).'/config/config.inc.php';
$cookie = new Cookie('psAdmin', '', (int)Configuration::get('PS_COOKIE_LIFETIME_BO'));

if (isset($cookie->id_employee) && $cookie->id_employee) {
    echo 'ID Employee: '.$cookie->id_employee;
} else {
    echo 'No employee logged in.';
}

Now you can verify if an employee is logged in the Back Office by calling this file:

http://domain.com/cookie_access.php

Hope this help you!

 

It seems to work, thank you!

Share this post


Link to post
Share on other sites

  • 1 year later...

This code seems to be correctly working on firefox. On chrome the id_employee's not being retrieved.

Cookie Object
(
    [_content:protected] => Array
        (
            [date_add] => 2018-05-14 14:00:30
            [id_lang] => 1
            [detect_language] => 1
            [employee_form_lang] => 1
            [id_employee] => 181
            [email] => [email protected]
            [profile] => 1
            [passwd] => b3db667d1eb09459668b87f7eda24f8d
            [remote_addr] => 1348153989
            [shopContext] => s-1
            [checksum] => 3082462495
        )
Cookie Object
(
    [_content:protected] => Array
        (
            [date_add] => 2018-05-14 14:10:04
            [id_lang] => 1
            [detect_language] => 1
        )

 

First one is firefox and second one is chrome ... any suggestions ? 

Share this post


Link to post
Share on other sites

  • 3 years later...
On 8/5/2016 at 9:13 PM, Rolige said:

The best way is enter in the ecosystem of PrestaShop through a file, controller, module, etc, etc.

 

This is an example with a simple file in the root....

 

Create a file in the root directory of the shop:

cookie_access.php

Add this code inside the file:

<?php

require_once dirname(__FILE__).'/config/config.inc.php';
$cookie = new Cookie('psAdmin', '', (int)Configuration::get('PS_COOKIE_LIFETIME_BO'));

if (isset($cookie->id_employee) && $cookie->id_employee) {
    echo 'ID Employee: '.$cookie->id_employee;
} else {
    echo 'No employee logged in.';
}

Now you can verify if an employee is logged in the Back Office by calling this file:

http://domain.com/cookie_access.php

Hope this help you!

thank you, but how to call this file inside my module main php file ?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More