How to check is URL https secure or not in php

In this quick post I am going to share single line of php code to detect is server https secured or not, If the request was sent with HTTPS you will have a extra parameter in the $_SERVER which give all the information about current request. So that you can easily check whether connection secure or not by running single line of php code. And take appropriate action over https / http.
moving-http-to-https
Following code will check is $_SERVER[‘HTTPS’] set and on or not If HTTPS is on then your current URL is secure and over https, Additionally check for secure SSH post 443 too.


<?= ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443) ? 'https' : 'http'; ?>

Above single line of php code will help you to get whether current URL over https or http.

Also Read: How to get free SSL certificate and transfer your website from http to https



Posted in PHP