Post by coldwellr » Mon Apr 09, 2018 9:00 pm

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.

Newbie

Posts

Joined
Fri Nov 17, 2017 9:07 pm

Post by rjcalifornia » Mon Apr 09, 2018 10:43 pm

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) ?>

Image


Active Member

Posts

Joined
Fri Sep 02, 2011 1:19 pm
Location - Worldwide

Post by straightlight » Tue Apr 10, 2018 4:58 am

Code: Select all

{%endif%}
for:

Code: Select all

{% endif %}

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Opencart.com contractor officials representative


Administrator

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by paulfeakins » Tue Apr 10, 2018 4:53 pm

@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);

For quick, professional OpenCart support please email info@antropy.co.uk


User avatar
Expert Member

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - Reigate, Surrey, United Kingdom

Post by coldwellr » Tue Apr 10, 2018 7:24 pm

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.

Newbie

Posts

Joined
Fri Nov 17, 2017 9:07 pm

Post by straightlight » Tue Apr 10, 2018 7:29 pm

** 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.
Last edited by straightlight on Sun Jul 07, 2019 9:35 pm, edited 15 times in total.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Opencart.com contractor officials representative


Administrator

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by coldwellr » Tue Apr 10, 2018 7:45 pm

@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.

Newbie

Posts

Joined
Fri Nov 17, 2017 9:07 pm

Post by straightlight » Tue Apr 10, 2018 8:20 pm

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.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Opencart.com contractor officials representative


Administrator

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by straightlight » Sun Mar 17, 2019 11:00 pm

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.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Opencart.com contractor officials representative


Administrator

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by letxobnav » Mon Mar 18, 2019 12:19 am

I would simply connect to the database and do a select on the oc_customer_online table.

Crystal Light Centrum Taiwan
“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


Active Member
Online

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by straightlight » Mon Mar 18, 2019 2:06 am

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.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Opencart.com contractor officials representative


Administrator

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by straightlight » Mon Mar 18, 2019 2:50 am

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.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Opencart.com contractor officials representative


Administrator

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by letxobnav » Mon Mar 18, 2019 8:39 am

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.

Crystal Light Centrum Taiwan
“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


Active Member
Online

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by straightlight » Mon Mar 18, 2019 9:03 am

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.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Opencart.com contractor officials representative


Administrator

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by straightlight » Sun Apr 07, 2019 5:09 am

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

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Opencart.com contractor officials representative


Administrator

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by straightlight » Sun Apr 07, 2019 6:30 am

Added language support from browser code and database.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Opencart.com contractor officials representative


Administrator

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by straightlight » Tue Jul 02, 2019 5:54 pm

Re-built API structure yesterday: viewtopic.php?f=202&t=203512&p=758841#p720446 . Now fully supporting OC registry identically with the OC startups.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Opencart.com contractor officials representative


Administrator

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by straightlight » Fri Jul 05, 2019 9:02 am

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 .

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Opencart.com contractor officials representative


Administrator

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON
Who is online

Users browsing this forum: No registered users and 35 guests