FILTER_VALIDATE_URL

We can validate a url by using FILTER_VALIDATE_URL function in PHP. Here is one simple example
$url='https://www.example'; //Change this value to check different URL

if(filter_var($url,FILTER_VALIDATE_URL)){
echo " Correct ,   URL Validation passed ";
}else{
echo " Wrong , URL Validation failed ";
}
In the above code we can get the message that Correct, URL Validation passed. Same way you can change the data and get the staus.

Optional Flags

FILTER_FLAG_SCHEME_REQUIRED - additional check of RFC compliant (like http://example)
FILTER_FLAG_HOST_REQUIRED - additional check of host name (like http://www.example.com
FILTER_FLAG_PATH_REQUIRED - additional check of path to domain name (like www.example.com/php_tutorial/)
FILTER_FLAG_QUERY_REQUIRED - With a query string (like "example.php?id=34&cat=student&class=Four")

FILTER_FLAG_SCHEME_REQUIRED & FILTER_FLAG_HOST_REQUIRED are deprecated in PHP 7.3 as they are included within FILTER_VALIDATE_URL.

Validating URL with path

We can add one optional flag FILTER_FLAG_PATH_REQUIRED to check if any path is added after the URL. Without presence of a path this validation will fail. This flag checks minimum one forward slash ( / ) after the URL to be present. Here is the code
if(filter_var($_GET['url'],FILTER_VALIDATE_URL,FILTER_FLAG_PATH_REQUIRED)){
echo " Correct ,   URL Validation passed ";
}else{
echo " Wrong , URL Validation failed ";
}
Here is the list of URL which will pass and which will fail in the validation.
https://example.com         // Wrong, URL Validation failed 
https://example.com/path/filename.htm  // Correct validation passed
https://example.com/                   // Correct validation passed

Validating URL with query string

With the optional flag of FILTER_FLAG_QUERY_REQUIRED we can validate URL only if a valid query string is present ( along with URL ) . Here is the code to check
if(filter_var($_GET['url'],FILTER_VALIDATE_URL,FILTER_FLAG_QUERY_REQUIRED)){
echo " Correct ,   URL Validation passed ";
}else{
echo " Wrong , URL Validation failed ";
}
Here is a list of URL which will pass and which will fail the URL validation process
https://example.com       // Wrong, URL Validation failed 
https://example.com?id=5        // Correct validation passed
https://example.com/             // Wrong, URL Validation failed 


plus2net.com




Post your comments , suggestion , error , requirements etc here .




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer