Why Php Filters Malfunction ?
Q1. This is surely an invalid url: http://gvdvb.com/?mail=ali&bus=wertdomainn#wronghttp://gvdvb.com/?mail=ali&bus=wert(
The final character renders the above as an invalid url according to this url filter: https://regexr.com/39nr7
So how come php validates this invalid url ? I get no alert that the url I input in the url input field in the form is an invalid url.
Url Filter
<form method="POST" name="textfield" id="textfield" action="">
<fieldset>
<label for="url">Url:</label><br>
<input type="text" name="url" id="domain" maxlength="255" size="20">
<br>
<button type="submit">Submit Now!</button>
</form>
<?php
if($_SERVER['REQUEST_METHOD'] === 'POST')
{
if(ISSET($_POST['url']))
{
if(!filter_input(INPUT_POST,'url',FILTER_VALIDATE_URL))
{
die('Invalid Url: ' .$_POST['url']); echo '<br>';
}
else
{
$url = $_POST['url'];
$parse = parse_url($url);
$domain = $parse['host'];
if(!filter_var($domain,FILTER_VALIDATE_DOMAIN))
{
die("Invalid Domain: $domain"); echo '<br>';
}
echo 'Valid Domain: ' .$domain;
}
}
}
Note that, on the html form, I put the domain input field as:
<label for="url">Url:</label><br>
<input type="text"
I did this deliberately so the html5 doesn't give the error. I wanted php to give the error instead as got to learn how to filter url with php.
Q2. Why doesn't php's domain filter work ? Whenever I input a invalid url with an invalid domain: xxx.
I get echoed: Valid Domain: LINE: 27
<form method="POST" name="textfield" id="textfield" action="">
<fieldset>
<label for="url">Url:</label><br>
<input type="text" name="url" id="domain" maxlength="255" size="20">
<br>
<button type="submit">Submit Now!</button>
</form>
<?php
if($_SERVER['REQUEST_METHOD'] === 'POST')
{
if(ISSET($_POST['url']))
{
$url = $_POST['url'];
$domain = parse_url($url,PHP_URL_HOST);
if(!filter_input(INPUT_POST,'url',FILTER_VALIDATE_DOMAIN))
{
echo "Invalid Domain: $domain"; echo '<br>';
echo 'LINE: ' . __LINE__;
}
else
{
echo 'Valid Domain: ' .$domain;
echo 'LINE: ' . __LINE__;
}
}
}
Very puzzling!