简体   繁体   中英

Check whether the URL scheme is HTTP or HTTPS

I'm using the following code to add http:// to the URL.

(substr(strtolower($url), 0, 7) == 'http://'?"":"http://").$url

but how can I check whether the original URL contains https ? I don't want to use an OR clause.

preg_match("@^https?://@", $url)
echo parse_url($url, PHP_URL_SCHEME);

文档https://www.php.net/manual/en/function.parse-url.php

Use preg_match and a regular expression on your url :

preg_match(^http(s)?://);

If it returns true, then your URL is ok, whether it uses http of https.

!empty($_SERVER['HTTPS']) ? 'https' : 'http'
strncmp($url, 'https:', 6) === 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM