20

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.

Kathir Sid Vel
  • 858
  • 1
  • 8
  • 16

4 Answers4

60

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();
starball
  • 20,030
  • 7
  • 43
  • 238
Joe Mastey
  • 26,809
  • 13
  • 80
  • 104
  • getStore()->isCurrentlySecure()): ?> – Kathir Sid Vel Nov 03 '10 at 14:02
  • I used this option as I like to use the inbuilt solution. I'm sure the other methods work as well. Thanks a lot to everyone who answered. – Kathir Sid Vel Nov 03 '10 at 14:03
  • not exactly sure why, but this doesn't seem to be working in IE8. All other browsers are ok. anyone else has similar issues? – Greg Jan 16 '13 at 10:17
  • @GergelyVarga it's unlikely to be IE8 itself. I'd suspect caching or some other variable instead – Joe Mastey Jan 22 '13 at 02:00
  • Agree. The error was in the "Custom Menu" module, but only IE8 complained about the unsecure URL on the secure page. – Greg Jan 22 '13 at 11:14
  • We can also use this , in case of store front template files, (i personally prefer this and strongly recommend) $isSecure = Mage::app()->getFrontController()->getRequest()->isSecure(); ($isSecure) ? : 'https://' : 'http://' – Haijerome Dec 14 '13 at 05:45
  • Joe nailed it. In particular, `isCurrentlySecure` in `"app/code/core/Mage/Core/Model/Store.php" line 1259`checks for both overwrite offload header in XML configuration and the standard check of HTTPS, e.g. `$_SERVER['HTTPS'] != 'Off';`. – Devy Oct 29 '14 at 20:04
5

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

Haijerome
  • 1,540
  • 16
  • 21
-4

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";
}
?>
SimonDowdles
  • 2,026
  • 4
  • 24
  • 36
-4

The best bet is as follows

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