Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether url contains http:// or https:// [duplicate]

Tags:

http

php

Possible Duplicate:
Check if the url is contains the http or https

What is the code to figure out whether the given URL contains http:// or https:// at the beginning using PHP and regular expressions?

like image 789
user398341 Avatar asked Sep 19 '11 08:09

user398341


People also ask

How do I know if my URL contains https?

We can use RegEx (Regular Expressions) to check if http or https exists in the URL string. But there is another quick way as well. We can use indexOf() to to check for the same.

How do I check if a string contains https?

PHP – Check if String Starts with http To check if string starts with “http”, use PHP built-in function strpos(). strpos() takes the string and substring as arguments and returns 0, if the string starts with “http”, else not. This kind of check is useful when you want to verify if given string is an URL or not.

How to check URL have http or https in php?

So, using the $_SERVER['HTTPS'] in isset() function, that is used to check whether it exists or not. This will also tell us whether HTTPS is enabled or not. Check the value of $_SERVER['HTTPS'], if it is “on”, then HTTPS is enabled and we have to append “https” to the URL.

Can a site be both http and https?

They can both be open at the same time, they can even serve different websites. In some ways they are 2 different websites. To avoid this you can simply close down port 80, or alternatively, make sure that website served on port 80 always sends a redirect to the https website.


1 Answers

you can use parse_url

<?php
$url = parse_url('https://example.org');

if($url['scheme'] == 'https'){
   // is https;
}
?>
like image 192
Mihai Iorga Avatar answered Sep 19 '22 18:09

Mihai Iorga