Page 1 of 1

Check for user login but outside of OpenCart

Posted: Mon Apr 09, 2018 9:00 pm
by coldwellr
I have OpenCart 3.0.2.0 running at 'example.com/shop'. How can I use a PHP file outside of OpenCart (i.e. 'example.com/file.php') to check if a user has logged in? Can I 'include' certain OpenCart core files in my 'file.php' to allow me to do this? All I really want to do is to check if a user has logged in and get their 'customer_id'. Any tips?

Many thanks.

Re: Check for user login but outside of OpenCart

Posted: Mon Apr 09, 2018 10:43 pm
by rjcalifornia
What you need to do is to add this in the controller:

$data['logged'] = $this->customer->isLogged();

And then check in Twig:
{% if logged != null %}

Do something...

{%endif%}

PHP:

<?php if (!$logged) ?>

Re: Check for user login but outside of OpenCart

Posted: Tue Apr 10, 2018 4:58 am
by straightlight

Code: Select all

{%endif%}
for:

Code: Select all

{% endif %}

Re: Check for user login but outside of OpenCart

Posted: Tue Apr 10, 2018 4:53 pm
by paulfeakins
@rjcalifornia and @straightlight you have not understood the question.

The question is how do you this in a separate file outside OpenCart?

You might be able to find something in the session:

Code: Select all

print_r($_SESSION);

Re: Check for user login but outside of OpenCart

Posted: Tue Apr 10, 2018 7:24 pm
by coldwellr
Hi. Yes the question was how to do this 'outside' of OpenCart.

I have now figured this out as follows:

I'm using ProcessWire as my CMS for my website (hence the external PHP file). ProcessWire uses its own session API (rather than $_SESSION) but the principle is the same. If I include the ProcessWire index.php file in 'catalog/controller/common/header.php' I am able to access the ProcessWire session and set a session variable like this...

Code: Select all

include(DIR_PROCESSWIRE . 'index.php');  // I've set this in the OpenCart config.php

if(isset($this->session->data['customer_id'])) {
// add a variable to the ProcessWire session using the 'wire' API    
$wire->session->set('oc_customer_id', $this->session->data['customer_id']);    

//get some website content data from ProcessWire
echo("<!--" . $wire->pages->get('/')->title . "-->"); 

// send data to ProcessWire session
echo("<!--" . $wire->session->get('oc_customer_id') . "-->"); 
} 
Now from within ProcessWire I am able to access the customer ID. I'm then using a class I've written to access the OpenCart database and retrieve the data I want about the customer (using AJAX/JASON to fetch and display the data on an 'account' page in ProcessWire).

Any thoughts on this technique would be welcome.

Thanks.

Re: Check for user login but outside of OpenCart

Posted: Tue Apr 10, 2018 7:29 pm
by straightlight
** UPDATE: JULY 4st 2019 **

From this day forward, the Opencart API example is now available on my Github namespace: https://github.com/straightlight/openca ... pload/api/ as it includes the customer logged in example. This example assumes you are using JSON GET requests as it includes the API backward compatibility for API security purposes. This should be the final release of this feature until the OC team releases new codes.

Re: Check for user login but outside of OpenCart

Posted: Tue Apr 10, 2018 7:45 pm
by coldwellr
@straightlight

That's awesome, thanks for this solution. This means I now have what I need to implement a reliable system that will do what I want. My main issue was how to get OpenCart and ProcessWire talking to each other. I have done some experimenting with the OpenCart API which allows me to login and logout, retrieve data etc, but does not allow me to check if an existing user has logged in themselves.

Thanks so much for your time on this, it is much appreciated.

Re: Check for user login but outside of OpenCart

Posted: Tue Apr 10, 2018 8:20 pm
by straightlight
No problem. I have also fixed:

Code: Select all

`customer_id` = '" . (int)$customer_id . "' AND `status` = '1'
To read:

Code: Select all

`c`.`customer_id` = '" . (int)$customer_id . "' AND `c`.`status` = '1'
from the code above.

Re: Check for user login but outside of OpenCart

Posted: Sun Mar 17, 2019 11:00 pm
by straightlight
I have now extended the codes above to provide safe passage throughout the Opencart API mechanism with the use of JSON for remote platforms. Obviously, only authorized OC users per stores are authorized with this example which means the API ID also needs to match your remote platform if planning to develop a remote API call with AJAX request with the key name: api_id as POST method. The IP address also needs to match with the API ID for security access.

Re: Check for user login but outside of OpenCart

Posted: Mon Mar 18, 2019 12:19 am
by letxobnav
I would simply connect to the database and do a select on the oc_customer_online table.

Re: Check for user login but outside of OpenCart

Posted: Mon Mar 18, 2019 2:06 am
by straightlight
letxobnav wrote:
Mon Mar 18, 2019 12:19 am
I would simply connect to the database and do a select on the oc_customer_online table.
That solution would be for local APIs. The solution provided on my previous post is about remote platforms. Besides, tracking the customer online table still requires to pass the customer ID through the browser either as a GET or POST method rather than assigning a token from API.

In addition, the customer online table is not running in real time mode which means a customer could just closed his browser and the customer online table would still show the customer ID on the database until expiry.

Re: Check for user login but outside of OpenCart

Posted: Mon Mar 18, 2019 2:50 am
by straightlight
I have now added a complete example by using a customer online token rather than obtaining the customer ID in order to remain on the safe side with the API. The rest is about improvising from the developers' end on how the customer online token will be sent back to their remote platform before the next step on verifying if the customer, with the gathered token, is still online.

Re: Check for user login but outside of OpenCart

Posted: Mon Mar 18, 2019 8:39 am
by letxobnav
I have OpenCart 3.0.2.0 running at 'example.com/shop'. How can I use a PHP file outside of OpenCart (i.e. 'example.com/file.php') to check if a user has logged in?
That is not on a different platform and does not require GET or POST, just DB credentials and a query.

When a customer logs out the record in the customer_online table is removed instantly, you could even run DB triggers on that.

Maybe I am missing something but you can never determine if a user closes their browser in real time otherwise we would not need session garbage collection.

Re: Check for user login but outside of OpenCart

Posted: Mon Mar 18, 2019 9:03 am
by straightlight
Maybe I am missing something but you can never determine if a user closes their browser in real time otherwise we would not need session garbage collection.
Which is why, a token is rather preferred to ensure remote platforms can get in without access issues. As for the DB query, if that were true, Opencart would not need to use backward compatibility as simply connecting to the database directly from the admin towards the catalog models would of suffice which, obviously for security reasons, is not the case here.

Re: Check for user login but outside of OpenCart

Posted: Sun Apr 07, 2019 5:09 am
by straightlight
I have expanded the API codes in order to be able to use more OC objects at the same time: viewtopic.php?f=202&t=203512&p=751413#p720446

Re: Check for user login but outside of OpenCart

Posted: Sun Apr 07, 2019 6:30 am
by straightlight
Added language support from browser code and database.

Re: Check for user login but outside of OpenCart

Posted: Tue Jul 02, 2019 5:54 pm
by straightlight
Re-built API structure yesterday: viewtopic.php?f=202&t=203512&p=758841#p720446 . Now fully supporting OC registry identically with the OC startups.

Re: Check for user login but outside of OpenCart

Posted: Fri Jul 05, 2019 9:02 am
by straightlight
The Opencart API is now available on my namespace on Github. Please read the updated post: viewtopic.php?f=202&t=203512&p=759094#p720446 .