This question already has answers here:
Closed 11 years ago.
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?
you can use parse_url
<?php
$url = parse_url('https://example.org');
if ($url['scheme'] == 'https') {
// is https;
}
?>
if (substr($string, 0, 7) == "http://") {
$res = "http";
}
if (substr($string, 0, 8) == "https://") {
$res = "https";
}
Maybe this could help
$_SERVER['SERVER_PROTOCOL'];
Related
This question already has answers here:
How to get the first subdomain with PHP?
(5 answers)
Closed 3 years ago.
I have the following code:
$url=$_SERVER['HTTP_HOST']; // www.abc.alpha.beta.xyz
$url=strtolower($url);
$rwww=str_replace("www.", "", $url);
But this results in abc.alpha.beta.xyz, the desired result is abc. How can I get just the first subdomain, ignoring www if present?
I think you can use the PHP strpos() function to check if the subdomain URL string contains the word alpha
// Search substring
$key = 'alpha';
$url = 'http://abc.alpha.beta.xyz';
if (strpos($url, $key) !== false) {
echo $key;
}
Not the best solution but might be helpful for you to get started.
There are a lot of ways to do that, a simple one being:
$host = $_SERVER['HTTP_HOST'] ?? 'www.abc.alpha.beta.xyz';
$parts = explode('.', $host);
$answer = null;
foreach ($parts as $subdomain) {
if ($subdomain === 'www') {
continue;
}
$answer = $subdomain;
break;
}
echo "The answer is '$answer'";
Which would output:
The answer is 'abc'
Be aware that this is a very naïve approach and will return example for the input www.example.com - which isn't a subdomain.
I am making a json validation that needs to validate url that starts with http:// or https://
if(preg_match("/^[http://][a-zA-Z -]+$/", $_POST["url"]) === 0)
if(preg_match("/^[https://][a-zA-Z -]+$/", $_POST["url"]) === 0)
Am I wrong in synatx, and also how should i combine both (http and https) in same statement ?
Thank you !
Use $_SERVER['HTTPS']
$isHttps = (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) ? true : false;
you can use parse_url
<?php
$url = parse_url($_POST["url"]);
if($url['scheme'] == 'https'){
// is https;
}else if($url['scheme'] == 'http'){
// is http;
}
// try to do this, so you can know what is the $url contains
echo '<pre>';print_r($url);echo '</pre>';
?>
OR
<?php
if (substr($_POST["url"], 0, 7) == "http://")
$res = "http";
if (substr($_POST["url"], 0, 8) == "https://")
$res = "https";
?>
If you want to check your string starts with http:// or https:// without worrying about the validity of the whole URL, just do that :
<?php
if (preg_match('`^https?://.+`i', $_POST['url'])) {
// $_POST['url'] starts with http:// or https://
}
This question already has answers here:
Get the full URL in PHP
(27 answers)
Closed 8 years ago.
How to get the current full url in php?
Ex. full url: www.topclinique.ma/list-cliniques.php?t=cliniques&s=0&c=Casablanca
You can use
$current_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Try this:
if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
$protocol = 'https://';
}
else {
$protocol = 'http://';
}
$current_link = $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $current_link;
I've working on a project and in this project i need to check the user input is a valid URL.
I've made a preg_match for all possible characters used on a URL. However, I'm trying to make it show an error if HTTP:// / HTTPS:// is not in front of the URL.
Here is what I've done.
if(preg_match('/[^0-9a-zA-Z.\-\/:?&=#%_]/', $url) || substr($url, 0, 7) != "http://" || substr($url, 0, 8) != "https://") {
But that doesn't work. It keeps giving me the an OK message. I'm not sure what I'm doing wrong here, I hope I can get some help!
The if statement will return true or false. So
if(preg_match('/[^0-9a-zA-Z.\-\/:?&=#%_]/', $url) || substr($url, 0, 7) != "http://" || substr($url, 0, 8) != "https://") {
echo "true";
} else {
echo "false";
}
I just need to check if the url has entered a valid url. I don't need to verify it. Just need to check if it has HTTP:// or HTTPS:// and contains valid URL characters.
Instead of a regex, you could make things easy on yourself and use the URL filtering in filter_var:
if (filter_var($url, FILTER_VALIDATE_URL)) { ...
Alternately you can do this without regex. Though you do also need to validate the url imagine http://">bla</a><script>alert('XSS');</script> as the value passed as there url
<?php
$url = 'http://example.com';
if(in_array(parse_url($url, PHP_URL_SCHEME),array('http','https'))){
if (filter_var($url, FILTER_VALIDATE_URL) !== false) {
//valid url
}else{
//not valid url
}
}else{
//no http or https
}
?>
parse_url()
filter_var()
You've not shown your complete relevant code. So, not sure, why it is not working for you but for url validation, you can check for a detailed discussion on the thread link below:
PHP validation/regex for URL
To validate user input with website url it is good to allow with or without scheme and with or without www, then in view add scheme to set as external url.
$withWww = 'www.' . str_replace(array('www.'), '', $value);
$withScheme = 'http://' . str_replace(array('http://', 'htttps://'), '', $withWww);
$headers = #get_headers($withScheme);
if (strpos($headers[0], '200') === false) {
return false;
}
This question already has answers here:
What is the best regular expression to check if a string is a valid URL?
(62 answers)
Closed 9 years ago.
How to make sure that a string contains a valid/well formed url?
I need be sure the url in the string is well formed.
It must contain http:// or https://
and .com or .org or .net or any other valid extension
I tried some of the answers found here in SO but all are accepting "www.google.com" as valid.
In my case the valid url needs to be http://www.google.com or https://www.google.com.
The www. part is not an obligation, since some urls dont use it.
Take a look at the answer here:
PHP regex for url validation, filter_var is too permisive
filter_var() could be just fine for you, but if you need something more powerful, you'll have to use regex.
Additionally with the code from here, you can sub-in any regex that suits your needs:
<?php
$regex = "((https?|ftp)\:\/\/)?"; // SCHEME
$regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?#)?"; // User and Pass
$regex .= "([a-z0-9-.]*)\.([a-z]{2,3})"; // Host or IP
$regex .= "(\:[0-9]{2,5})?"; // Port
$regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path
$regex .= "(\?[a-z+&\$_.-][a-z0-9;:#&%=+\/\$_.-]*)?"; // GET Query
$regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor
?>
Then, the correct way to check against the regex list as follows:
<?php
if(preg_match("/^$regex$/", $url))
{
return true;
}
?>
YOU can do this by using php filter_var function
$valid=filter_var($url, FILTER_VALIDATE_URL)
if($valid){
//your code
}
there's a curl solution:
function url_exists($url) {
if (!$fp = curl_init($url)) return false;
return true;
}
and there's a fopen solution (if you don't have
function url_exists($url) {
$fp = #fopen('http://example.com', 'r'); // #suppresses all error messages
if ($fp) {
// connection was made to server at domain example.com
fclose($fp);
return true;
}
return false;
}
filter_var($url, FILTER_VALIDATE_URL) can be first used to make sure you are dealing with a valid URL.
Then you have more conditions that can be tested by assuming the URL is indeed valid with parse_url:
$res = parse_url($url);
return ($res['scheme'] == 'http' || $ret['scheme'] == 'https') && $res['host'] != 'localhost');