简体   繁体   中英

Can I check for HTTPS in Magento PHTML files?

I am installing Clicky code on a Magento website. I would like to use their HTTPS tracker only on HTTPS enabled pages of Magento. How can I do this?

I tried

<?php if($_SERVER['https'] == 'on') : ?>

but that doesn't work.

Any suggestions on identifying HTTPS pages will be of great help!

Thanks.

Magento actually provides a method for this for you.

Use this to check whether you are in secure mode:

// check to see if your store is in secure mode
$isSecure = Mage::app()->getStore()->isCurrentlySecure();

Hope that helps!

Thanks, Joe

Native Magento solution

$isSecure = Mage::app()->getFrontController()->getRequest()->isSecure(); 
($isSecure) ? 'https://' : 'http://'; 

This helps to check whether your store front is in https or http

This may seem like a bit of a "hack" but you could check the server protocol and check for the existence of the characters "HTTPS" in the protocol? :

<?php 
$protocol = $_SERVER['SERVER_PROTOCOL'];
$protocol = substr($protocol,0,5); //will return something like HTTP/ or HTTPS
if(preg_match("^HTTPS^",$protocol)){
echo "ITS HTTPS";
}
?>

最好的选择如下

<?php if( $_SERVER['HTTPS'] || strtolower($_SERVER['HTTPS']) == 'on' ){  /* HTTPS */ } else{ /* NOT SO HTTPS */ } ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM