Link to home
Start Free TrialLog in
Avatar of KaranGupta
KaranGupta

asked on

http or https in PHP

Hi

How can I check if the current URL is HTTP or https or tcp in PHP?
ASKER CERTIFIED SOLUTION
Avatar of tailoreddigital
tailoreddigital
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gr8gonzo
You also mentioned "or tcp" but that is a bit vague. HTTP and HTTPS are protocols that sit on top of TCP. TCP defines the method of transmitting data, while HTTP and HTTPS define the content of that data.

So if you have some other type of protocol you want to check besides HTTP or HTTPS, you'll have to spell it out.

Also, if you're referring to things like something://blah blah and somethingelse://etc, then you should know that the <something>:// prefix of a URL is not really sent to the server, so the server doesn't know what it is. The <something> simply tells your computer how to treat everything after the ://, so when you have a URL like https://site.com, your computer sees "https" and knows that "https" is handled by a web browser, so your web browser takes over and does the rest.
Install this script, shown here in its entirety and run it with both HTTP and HTTPS.  Look in the output near the bottom for $_SERVER["HTTPS"].
<?php phpinfo();

Open in new window

Usually this test is sufficient to make a real-time programmatic determination
if (empty($_SERVER["HTTPS"])) { 
    $protocol = 'http://'; 
} else { 
    $protocol = 'https://'; 
}

Open in new window