Home > Magento > How to Detect if the Page is Secure in a Magento Template?

How to Detect if the Page is Secure in a Magento Template?

October 22nd, 2009

When you are designing a Magento template it is easy to overlook a simple fact: most links should not be hard-coded and you should always use the methods that are available for you like getSkinUrl() and getJsUrl(). In addition to pointing to the right root folder, these methods also point to the secure or unsecure URLs, ‘https’ and ‘http’ respectively. However, how can you detect if a URL needs to be secured or not?

herring-hall-marvin-safe

In a recent Magento project, I needed to do exactly that: find out if we are in secure or in a non secure page and point to the right jQuery file on Google CDN’s. Here is the code:

<?php if(Mage::app()->getStore()->isCurrentlySecure()): ?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script> 
<?php else: ?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script> 
<?php endif;  ?>

The key method here is the Mage::app()->getStore()->isCurrentlySecure().

Reblog this post [with Zemanta]

Magento , ,

Comments are closed.