Join my Laravel for REST API's course on Udemy 👀

Check if PHP script is served over HTTP or HTTPS

June 15, 2022  ‐ 1 min read

We can find out whether a request is made to our PHP script over HTTP or HTTPS by checking whether $_SERVER['HTTPS'] is set. This value in the $_SERVER superglobal is set to a non-empty value if the script runs on HTTPS.

Therefore we can check whether this value has been set or not, based on this we can determine if the script is server over HTTP or HTTPS.

<?php

$protocol = isset($_SERVER['HTTPS']) ? 'https' : 'http';
echo $protocol;
// 'http'