Shopping cart

How to check url is secured https, ssl in Magento 2

If you are finding the best answer to the question How to check url is secured https, SSL when Magento 2 platform is applied for your store, this topic is the right place to give you the perfect solution. How about the overview of the topic? Mageplaza team assists you to check if URL is secured https, ssl through the code snippets.

Overview of checking the url is secured https, ssl in Magento 2

  • Step 1: Declare in Mageplaza_HelloWorld
  • Step 2: Declare function in template .phtml file

Step 1: Declare in Mageplaza_HelloWorld

You will use a block class of the module Mageplaza_HelloWorld, then possibly inject the object of StoreManagerInterface in the constructor of the module’s block class.

app/code/Mageplaza/HelloWorld/Block/HelloWorld.php

<?php
namespace Mageplaza\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
    protected $_storeManager;    
    
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Store\Model\StoreManagerInterface $storeManager,        
        array $data = []
    )
    {        
        $this->_storeManager = $storeManager;        
        parent::__construct($context, $data);
    }
    
    /**
     * Check if frontend URL is secure
     *
     * @return boolean
     */
    public function isFrontUrlSecure()
    {
        return $this->_storeManager->getStore()->isFrontUrlSecure();
    }
    
    /**
     * Check if current requested URL is secure
     *
     * @return boolean
     */    
    public function isCurrentlySecure()
    {
        return $this->_storeManager->getStore()->isCurrentlySecure();
    }    
}
?>

You can see more functions in vendor/magento/module-store/Model/Store.php.

Step 2: Declare function in template .phtml file

Run the below function in your template .phtml file

var_dump($block->isFrontUrlSecure()) . '<br />';
var_dump($block->isCurrentlySecure()) . '<br />';

All above steps are the necessary things you need to do. If that you have any queries about the article or any questions in general, use the comment section below!

Related Podst





Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
check
url
secured
https
ssl

Brian Tran
Brian Tran
Senior Magento Developer who is passionate about development and love technology

Comments for How to check url is secured https, ssl in Magento 2

Please leave comments if you have any questions, feedbacks.

Module Development

We recommend:

You also may like these Magento 2 Extensions

One Step Checkout

$199
120 reviews

Layered Navigation

$99
61 reviews

Gift Card

$199
8 reviews

Reward Points

$99
8 reviews

Affiliate

$149
16 reviews

Shop By Brand

$99
19 reviews

People also searched for:

  • magento 2 check url is secured https ssl
  • check url is secured https ssl in magento 2
  • how to check url is secured https ssl
  • how to check url is secured https ssl in magento 2