If this is your first visit, check our documentation. To post, register or login.
Forums / Code Matters / Zen Cart Code Suggestions / Proposal: use PHP to redirect to https

Proposal: use PHP to redirect to https

Started by carlwhat on 27 Sep 2017, 1:02 AM · Views: 169

Results 1 to 7 of 7
27 Sep 2017, 1:02 AM
#1
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,974
Plugin Contributions:
8

Proposal: use PHP to redirect to https

for zc 1.5.5 (and/or beyond);

with some store owners looking to get their whole site https, i think there is an element that is missing. there is a page (that i could not find) that says to make the whole site https to change the configure to something like:

define('HTTP_SERVER', 'https://example.com');
define('HTTPS_SERVER', 'https://example.com');

with regards to the enable_ssl constant,the suggestion is to keep it set to true, requiring some overhead, but then payment modules that look for that constant do not get disabled.

the missing element is when someone lands on a non-https page from outside the domain, and then store owners are forced to make changes to htaccess file to do a redirect to the https page. and ZC does not come with a htaccess file as part of the base package.

with some add-ons using ajax to add-to-cart, if the landing page is non-https, and the redirects do not exist in the htaccess file, it could be a problem.

why not do the redirect from within ZC and minimize these problems from store owners? for example, from within includes/application_top.php and after the include of includes/configure.php, we could add something like:

if ((HTTPS_SERVER == HTTP_SERVER) && (strpos(HTTP_SERVER, 'https:') !== false)) {
    if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == "off") {
        $redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        header('HTTP/1.1 301 Moved Permanently');
        header('Location: ' . $redirect);
        exit();
    }
}

i think this would solve the landing page issue and a lot of confusion for some store owners with regards to making the whole site https, and having to do apache redirects in the htaccess file.

thoughts?

author of square Webpay, now with Apple Pay and Google Pay.
mXworks
donations: venmo or paypal accepted.
premium consistent excellent support. available for hire.

27 Sep 2017, 10:04 AM
#2
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Proposal: use PHP to redirect to https

The "missing" page is: https://www.zen-cart.com/content.php?56-how-do-i-enable-ssl-after-i-have-installed-zen-cart and yes the article has been changed to reference the setting of true as the generally preferred setting now-a-days I assume because of the number of related issues and that it would just be easier for the masses to leave it set to true rather than troubleshoot.

As for the concept of a potentially switch enabled internal redirect based on expected page load and current state, I see it as an improvement. I would think the current state would be through the test of $request_type which is comprised of several $_SERVER factors not just the ones presented above and similar internal reviews.

The reason for an additional admin switch is to prevent the misconfiguration of the server which is outside the control of ZC from preventing store operation. Certainly zc_install could attempt to verify operation; however, server settings on the existing server or on a server to which the site is moved could be different and having such an option in the admin should better support "recovery" from the host's issues.

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

27 Sep 2017, 11:40 AM
#3
soxophoneplayer avatar

soxophoneplayer

Totally Zenned

Join Date:
Feb 2008
Posts:
534
Plugin Contributions:
0

Re: Proposal: use PHP to redirect to https

mc12345678:

The "missing" page is: https://www.zen-cart.com/content.php?56-how-do-i-enable-ssl-after-i-have-installed-zen-cart and yes the article has been changed to reference the setting of true as the generally preferred setting...

My (very limited) understanding is that full site ssl wants the 'true' to be changed to 'false'. I'm sorry I can't find the ref - I believe the 'true' referred to making the original pages designed to accommodate ssl - login, checkout etc. but not ALL pages. So the 'true' means: go ssl but limit it to those pages, while 'false' means don't limit ssl to only the pages historically. Sort of the opposite of a double negative.

I struggled with this when going ssl and 'I think' it was Dr B who clarified the confusing meaning of 'true'.

So in the end, I did the two https server urls in the config, and 'false' and added a redirect in the root htaccess.

Since I am using a picaflor template (Responsive Sheffield Blue) I also did a search for 'http' in Developers Tools - it generated thousands to instances but with very few exceptions these were pulled out of credits in top of each file. The number that were actually in the code were just a handful and I manually changed these.

27 Sep 2017, 12:06 PM
#4
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Proposal: use PHP to redirect to https

soxophoneplayer:

My (very limited) understanding is that full site ssl wants the 'true' to be changed to 'false'. I'm sorry I can't find the ref - I believe the 'true' referred to making the original pages designed to accommodate ssl - login, checkout etc. but not ALL pages. So the 'true' means: go ssl but limit it to those pages, while 'false' means don't limit ssl to only the pages historically. Sort of the opposite of a double negative.

I struggled with this when going ssl and 'I think' it was Dr B who clarified the confusing meaning of 'true'.

So in the end, I did the two https server urls in the config, and 'false' and added a redirect in the root htaccess.

Since I am using a picaflor template (Responsive Sheffield Blue) I also did a search for 'http' in Developers Tools - it generated thousands to instances but with very few exceptions these were pulled out of credits in top of each file. The number that were actually in the code were just a handful and I manually changed these.
The recommendation to set SSL_ENABLE to false when using all SSL has been removed from the link that I provided in my previous post though there are many threads that still discuss it and provide the explanation.

Historically and even still in its design, ZC would serve pages up using https: if 1) it was a page designated as one that should be encrypted based on the content being posted and 2) the site supported https: which is/was identified by SSL_ENABLED being set to true. Now that the push is to have the entire site https:, the need to switch from one to the other is no longer there. The problem is that older versions of ZC and/or plugins did not anticipate https being included in HTTP_SERVER as an identification that the site was operating with an SSL.

The site being full https: does not require ENABLE_SSL to specifically be one or the other, but for the general operation of historical sites it is best at this time to keep it true when it is possible for a site to be served via https:. There is a processing improvement if all of the software currently installed supports secure operation when it is set to false, but the most users gain success of site operation by keeping it set to true when https: is possible.

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

27 Sep 2017, 3:46 PM
#5
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,974
Plugin Contributions:
8

Re: Proposal: use PHP to redirect to https

soxophoneplayer:

My (very limited) understanding is that full site ssl wants the 'true' to be changed to 'false'. I'm sorry I can't find the ref - I believe the 'true' referred to making the original pages designed to accommodate ssl - login, checkout etc. but not ALL pages. So the 'true' means: go ssl but limit it to those pages, while 'false' means don't limit ssl to only the pages historically. Sort of the opposite of a double negative.

I struggled with this when going ssl and 'I think' it was Dr B who clarified the confusing meaning of 'true'.

So in the end, I did the two https server urls in the config, and 'false' and added a redirect in the root htaccess.

Since I am using a picaflor template (Responsive Sheffield Blue) I also did a search for 'http' in Developers Tools - it generated thousands to instances but with very few exceptions these were pulled out of credits in top of each file. The number that were actually in the code were just a handful and I manually changed these.

my vague recollection was that there was a separate page for enabling SSL. it seems that has been incorporated into the main SSL page. no problem.

the problem with setting enable_ssl to false is pretty clear in the following code and why it is no longer recommended:

if (!defined('ENABLE_SSL') || ENABLE_SSL != 'true') $this->enabled = FALSE;

code similar to this is in any number of payment modules associated with ZC. if you set enable_ssl to false, certain payment modules will not be available in ZC. if you do not use those payment modules, no problem. if you do use those payment modules with this code, then you run into a problem. even though the whole site is served in https.

my code would eliminate the need for store owners with limited understanding of apache redirects to have a need to add anything into the htaccess file for serving the whole site in https. and that is why i am recommending it.

author of square Webpay, now with Apple Pay and Google Pay.
mXworks
donations: venmo or paypal accepted.
premium consistent excellent support. available for hire.

4 Oct 2017, 2:56 PM
#6
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Re: Proposal: use PHP to redirect to https

carlwhat:

with some add-ons using ajax to add-to-cart, if the landing page is non-https, and the redirects do not exist in the htaccess file, it could be a problem.
Why are those ajax calls hitting a "non-https landing page" in the first place?

carlwhat:

why not do the redirect from within ZC and minimize these problems from store owners? for example, from within includes/application_top.php and after the include of includes/configure.php, we could add something like:

if ((HTTPS_SERVER == HTTP_SERVER) && (strpos(HTTP_SERVER, 'https:') !== false)) {
if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == "off") {
$redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $redirect);
exit();
}
}

> 
> i think this would solve the landing page issue and a lot of confusion for some store owners with regards to making the whole site https, and having to do apache redirects in the htaccess file.
> 
> thoughts?
I'm not opposed to the idea, but the proposed code will only work correctly on a subset of server configurations. There are many configurations where the $_SERVER['HTTPS'] setting doesn't exist but the request is indeed an https request. There are about a dozen checks that need to be done, else you'll put the site into an endless loop of redirects.
Many of the factors collected over the years are contained in the check for $request_type, but this may not be a fully exhaustive list: <https://github.com/zencart/zencart/blob/v160/includes/init_includes/init_file_db_names.php#L16-L30>

I wouldn't put it into application_top. Rather, make it its own init_includes file.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

5 Oct 2017, 2:08 AM
#7
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,974
Plugin Contributions:
8

Re: Proposal: use PHP to redirect to https

ok....

with regards to the ajax request, it looks to be my mistake.... if one hits a non-https landing page, a properly implemented ZC site will use the HTTP_SERVER variable for the form submit. when jquery (or javascript) intercepts that request, and makes the ajax call, it should stay with whatever request type the form action currently is.... so for a properly coded ZC site, this should not be a problem. my mistake.

with regards to the coding, i still think it is a good idea as it will help store owners without having to mess with apache redirects in a htaccess file.

with regards to how it should load, it would require 2 new files (as opposed to the application top). an idea which i completely agree...

includes/init_includes/init_https.php
includes/auto_loaders/config.https.php

i set the config to load AFTER the init_file_db_names so as to not get too far down the road, as well as make use of the request type.

i think if we put it out into the wild, we can see what other servers are out there to further redefine the $request_type.

shall i initiate a PR on v160?

although after thinking about it, i would need to add another auto_loader on the admin side.

am i missing something?

finally, in reviewing some of the developers docs, i came across this tidbit:

"The goal of v1.3.x is to eventually remove and refactor all functions into classes. Further, these classes will be common between admin and catalog code."

a goal i have previously endorsed and which i am a huge fan.... and of which we can hopefully make even more progress in v160.

best.

author of square Webpay, now with Apple Pay and Google Pay.
mXworks
donations: venmo or paypal accepted.
premium consistent excellent support. available for hire.