Can I check for HTTPS in Magento PHTML files?

Can I check for HTTPS in Magento PHTML files?

July 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Can I check for HTTPS in Magento PHTML files?

Checking for HTTPS in Magento PHTML Files

As a senior developer working with Magento e-commerce platforms, I frequently encounter queries about checking for secure (HTTPS) connections within PHTML files. In this post, we’ll explore how to accurately identify HTTPS-enabled pages and implement Clicky’s HTTPS tracker.

Understanding the HTTPS Check

When configuring click tracking on your Magento site using Clicky’s JavaScript code, it’s crucial to ensure that the tracker only runs on secure (HTTPS) pages. This is not only a best practice for data security but also helps avoid mixed content warnings in modern browsers.

The Incorrect Approach: $_SERVER[‘https’]

You attempted to use the PHP superglobal array $_SERVER to check if the ‘https’ variable was set to ‘on’. However, this approach is flawed because Magento doesn’t actually use ‘https’ as a server variable. Instead, it relies on other variables like HTTPS and HTTPS_HOST to manage secure connections.

The Correct Approach: Using HTTPS Variables

To correctly identify HTTPS pages in your PHTML files, you should check the presence of the HTTPS constant defined by Magento. Here’s how to do this:

  1. First, ensure that the server variable $_SERVER['HTTPS'] is set appropriately based on your configuration. In most cases, it will either be ‘on’ (for secure) or left empty (for insecure).

  2. Next, in your PHTML file, you can create a conditional block to check if the HTTPS constant is defined:

<?php if (defined('HTTPS')) : ?>
    <!-- Your HTTPS code here -->
<?php else : ?>
    <!-- Your non-HTTPS code here -->
<?php endif; ?>

Integrating Clicky’s HTTPS Tracker

Once you’ve identified that a page is secure, you can include Clicky’s JavaScript tracker with the clicky_secure parameter set to true:

<script type="text/javascript">
  var clicky_site_ids = [YOUR_CLICKY_SITE_ID];
  (function() {
    var s = document.createElement('script');
    s.type = 'text/javascript';
    s.async = true;
    s.src = '//static.getclicky.com/js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(s);
  })();
</script>
<noscript><p><img alt="Clicky" width="1" height="1" src="//in.getclicky.com/<?php echo $this->escapeHtml($this->getUrl('no Script Clicky')); ?>" /></p></noscript>

Replace YOUR_CLICKY_SITE_ID with your actual Clicky site ID. The presence of the HTTPS constant in PHP will ensure that this JavaScript is only included on secure pages.

Conclusion

In summary, to check for HTTPS-enabled pages in Magento PHTML files:

  1. Use the defined('HTTPS') conditional statement.
  2. Implement Clicky’s tracker with the clicky_secure parameter set based on your security status check.

Remember, always consult Magento’s official documentation and ensure you’re compliant with their coding standards when modifying core templates or adding third-party scripts.

For more detailed guides on Magento development best practices, I recommend exploring resources like Laravel Company for expert insights and real-world examples of e-commerce optimization projects.

Security Themes Configuration JavaScript

Our network