Why Php Filters gone all bonkers ?
Look ...
An invalid url:
http://gvdvb.com/?mail=ali&bus=wertdomainn#wronghttp://gvdvb.com/?mail=ali&bus=wert(
The last char made the url invalid. We can check here ....
https://regexr.com/39nr7
So how come php validated this wrong url, hey ?
See here. And you will see with the below code you will get no alert that the url you input in the url input field (in the form) is invalid url.
ISSUE 1: 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 deliberatly wrote the domain input field type as:
<input type="text"
I did this deliberately so the html5 doesn't give the error as I need php to give the error instead. For me to learn how to filter url with php.
ISSUE 2: DOMAIN FILTER.
And check this one out too!
Can we make the php's domain filter work or not ?
You see, whenever I input an invalid url with an invalid domain such as: xxx.
I get echoed:
Valid Domain: LINE: 27
Notice I typed no tld. No dot either. Now how the heck is that a valid domain name ? Ridiculous!
<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 confusing! Yah ?