How To Check URL Protocol Is https by PHP

Hi friends, Today in this article I am writing about how can we check or found that URL is using https protocol or not by PHP. It is very simple in PHP to find this but as this is small thing, So it is obvious that most of people dont have any attension on it. Many programmer trying to find this thing by getting URL by $_SERVER veriable and then searching https in that url. But as I said it is very easy to check https is on or not in PHP.

We can find https is on or not by $_SERVER. There is one option in $_SERVER global array by which we can find that current URL having https or not. Below is code by which you can check URL using https or not.

if(isset($_SERVER['HTTPS']))
{
// code for https url go here
}
else
{
// code for http url go here
}

In above code you can see that I have checked isset($_SERVER[‘HTTPS’]) because if URL having https then it will return ‘on’ and if URL will have http then it will not return anything. So I have checked isset($_SERVER[‘HTTPS’])), So If URL using https it will go to if condition and if it is not using https then it will go to else condition.

Subscribe to PHP Freelancer

Enter your email address: