PHP Get Site Url Protocol - Http VS Https

PHP Get Site URL Protocol - http vs https

It is not automatic. Your top function looks ok.

How do retrieve a URL protocol ( http or https )?

I can't figure out something smarter than assuming "http://" as default and, if it fails, try "https://"

if (!$html = file_get_html('http://' . $url)) $html = file_get_html('https://' . $url);

How to get the protocol in php?


$protocol = current(explode('/',$_SERVER['SERVER_PROTOCOL']));

If you really just want to know if it HTTPS is enabled or not, you can just use $_SERVER['HTTPS'].

Identify whether the current url is http or https in a project


if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") { 
echo "something";
} else {
echo "something other";
}

notice the on should be a string .

Is a protocol (eg. http or https) required for a URL to be valid?

They're both valid scheme-relative-URL strings, although they need to be in the context of a Base URL to be meaningful. When used within a web page, the web page will provide the Base URL context.

Although there are other, earlier standards for URLs, the whatwg document represents the most up-to-date, web compatible definition.



Related Topics



Leave a reply



Submit