/    Sign up×
Community /Pin to ProfileBookmark

Why Php Domain Filter No Working ?

Folks,

Did I write my code right ?

““
<?php

$url = ‘http://webdeveloper’;
$domain = parse_url($url,PHP_URL_HOST);

if(!filter_var($domain,FILTER_VALIDATE_DOMAIN))
{
echo “Invalid Domain: $domain”; echo ‘<br>’;
echo ‘LINE: ‘ . __LINE__;
}
else
{
echo ‘Valid Domain: ‘ .$domain;
echo ‘LINE: ‘ . __LINE__;
}

?>
““

I get echoed:
**Valid Domain: webdeveloperLINE: 15**

I should’ve got echoed: **Invalid Domain: webdeveloperLINE: 15**

It seems aslong as the url contains “http://” regardless if the url contains a tld or not, the php filter accepts it as a valid domain.
Here’s the proof ….

““
<?php

$url = ‘webdeveloper.com’;
$domain = parse_url($url,PHP_URL_HOST);

if(!filter_var($domain,FILTER_VALIDATE_DOMAIN))
{
echo “Invalid Domain: $domain”; echo ‘<br>’;
echo ‘LINE: ‘ . __LINE__;
}
else
{
echo ‘Valid Domain: ‘ .$domain;
echo ‘LINE: ‘ . __LINE__;
}

?>
““

This time, I get echoed:
**Invalid Domain:
LINE: 10**

I should’ve got echoed:
**Valid Domain: webdeveloper.com
LINE: 10**

How to fix this mess of the php vendor ? How would you code it to overcome their mess ?

**NOTE:
The invalid domain on the 2nd example is not getting echoed. Why is that ?**

Cheers!

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmSep 17.2021 — Makes sense to me. No way that php can reach out and search the world for a use of whatever domain you are entering. All it can do is see if it fits the syntax. Kind of like an email address being verified. Can't send out an email and see if a reply comes back, can it? Or checking a phone number as a simpler case. If it's all numbers then it may very well be a phone number, although it could be a 'wrong number'.
Copy linkTweet thisAlerts:
@developer_webauthorSep 17.2021 — @ginerjm#1637159

I didn't say php should go and check whether the domain exists or not. I'm talking about syntax. How is this a valid domain syntax ?

**http://webdeveloper**

And how is this an invalid domain syntax ?

webdeveloper.com

Tell me atleast that.

In short, this is malfunctioning:
<i>
</i>filter_var($domain,FILTER_VALIDATE_DOMAIN
Copy linkTweet thisAlerts:
@ginerjmSep 17.2021 — As I said I don't believe that php can check for a valid domain. It only checks for the syntax that is presented. Please note that your filter_var is being checked as NOT so perhaps your messages are backwards?_

Here is my test code
<i>
</i>//Test #1
$url = 'http://webdeveloper.com';
echo "Test #1&lt;br&gt;";
if ($domain = parse_url($url))
echo "For url of $url:&lt;pre&gt;",print_r($domain,true),"&lt;/pre&gt;";
else
echo "url host check failed&lt;br&gt;";

if($domainname = filter_var($domain['host'],FILTER_VALIDATE_DOMAIN))
echo "For domain of {$domain['host']} check is valid&lt;br&gt;";
else
echo "For domain of {$domain['host']} check of $domainname failed&lt;br&gt;";
//**************
//Test #2
$url = 'http://webdeveloper';
echo "Test #2&lt;br&gt;";
if ($domain = parse_url($url))
echo "For url of $url:&lt;pre&gt;",print_r($domain,true),"&lt;/pre&gt;";
else
echo "url host check failed&lt;br&gt;";

if($domainname = filter_var($domain['host'],FILTER_VALIDATE_DOMAIN))
echo "For domain of {$domain['host']} check is valid&lt;br&gt;";
else
echo "For domain of {$domain['host']} check of $domainname failed&lt;br&gt;";

The results of this are:

Test #1

For url of http://webdeveloper.com:

Array

(

[scheme] => http

[host] => webdeveloper.com

)

For domain of webdeveloper.com check is valid

Test #2

For url of http://webdeveloper:

Array

(

[scheme] => http

[host] => webdeveloper

)

For domain of webdeveloper check is valid

Which to me seems correct. Syntax.
Copy linkTweet thisAlerts:
@developer_webauthorSep 21.2021 — @ginerjm#1637164

Ok. Thanks for the marvellous code.

But, since the 2nd test domain doesn't have a tld should not php flag a red light here. That's what I originally meant.
Copy linkTweet thisAlerts:
@NogDogSep 21.2021 — See the manual for what that filter _actually_ does (as opposed to what you think it should do): https://www.php.net/manual/en/filter.filters.validate.php
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...