[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 339
  • Last Modified:

http or https in PHP

Hi

How can I check if the current URL is HTTP or https or tcp in PHP?
0
KaranGupta
Asked:
KaranGupta
1 Solution
 
gr8gonzoConsultantCommented:
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.
0
 
Ray PaseurCommented:
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

0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now