/    Sign up×
Community /Pin to ProfileBookmark

Why FILTER_VALIDATE_DOMAIN Fails ?

Php Gurus,

It seems according to NotePad++, FILTER_VALIDATE_DOMAIN does not exist. ut ased on this ir does:
https://www.php.net/manual/en/filter.filters.validate.php

So now how to validate domains ?
This is what I tried …

““
<!DOCTYPE HTML>

<html>

<head>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
</head>

<body>

<form method=”GET” name=”submit_link” id=”submit_link” action=<?php echo $_SERVER[‘PHP_SELF’];?>>
<label for=”url”>Url *</label>
<input type=”url” name=”webpage_address” id=”url” placeholder=”Type here your webpage address …” REQUIRED>
<br>
<label for=”email”>Email *</label>
<input type=”email” name=”email_address” id=”email” placeholder=”Type here your email address belonging to your domain …” REQUIRED>
<br>
<label for=”domain”>Domain *</label>
<input type=”domain” name=”domain” id=”domain” placeholder=”Type here your domain belonging to your submitted url …” REQUIRED>
<br>
<button name=”search” id=”search” value=” “>Submit</button>
<br>
<input type=”reset”>
</form>

</body>

</html>
<?php

$submitted_url = $_GET[‘url’];
$submitted_domain = $_GET[‘domain’];
$submitted_email = $_GET[’email’];

if(FILTER_VALIDATE_URL($submitted_url) === ‘FALSE’)
{
die(“Input a VALID url!”);
}
if(FILTER_VALIDATE_EMAIL($submitted_email) === ‘FALSE’)
{
die(“Input a VALID email address!”);
}
if(FILTER_VALIDATE_DOMAIN($submitted_domain) === ‘FALSE’)
{
die(“Input a VALID domain address!”);
}
?>
““

Show me an example how to validate domains.

PS – The programmers are in a fix how to validate domains. Check here:
https://stackoverflow.com/questions/1755144/how-to-validate-domain-name-in-php

Which code sample should I stick to ? Which one will you stick to ? Or maybe you got your own which is a etter one ? In that case let us see what you got.

Thanks

to post a comment
PHP

20 Comments(s)

Copy linkTweet thisAlerts:
@NachfolgerSep 03.2020 — >It seems according to NotePad++, FILTER_VALIDATE_DOMAIN does not exist. ut ased on this ir does: https://www.php.net/manual/en/filter.filters.validate.php

FILTER_VALIDATE_URL is not a function, it's a filter_var flag. Additionally, don't test validate functions against a string. **That's incorrect**.

Unequivocally wrong:
``PHP<i>
</i>filter_var(...) === "FALSE"<i>
</i>
`</CODE>

Correct usage of <C>
FILTER_VALIDATE_URL</C> flag:
<CODE lang="PHP">
`PHP<i>
</i>filter_var("https://www.google.com", FILTER_VALIDATE_URL)<i>
</i>
`</CODE>

Correct implementation of <C>
FILTER_VALIDATE_URL</C> and <C>filter_var</C>:
<CODE lang="PHP">
`PHP<i>
</i>if (filter_var("https://www.google.com", FILTER_VALIDATE_URL) == 1) {
echo "valid url";
} else {
echo "invalid url";
}<i>
</i>
``


The same applies to all of your "implementations" of the flags you have in the code.

See also:

https://www.w3schools.com/php/func_filter_var.asp
Copy linkTweet thisAlerts:
@developer_webauthorSep 03.2020 — @Nachfolger#1622954

Thanks. At the end, I did headover to:

https://www.w3schools.com/php/func_filter_var.asp

Now my code looks like this:

<?php

$submitted_url = $_GET['url'];
$submitted_domain = $_GET['domain'];
$submitted_email = $_GET['email'];

if(FILTER_VALIDATE_URL($submitted_url) === 'FALSE')
{
die("Input a VALID url!");
}
if(FILTER_VALIDATE_EMAIL($submitted_email) === 'FALSE')
{
die("Input a VALID email address!");
}
if(FILTER_VALIDATE_DOMAIN($submitted_domain) === 'FALSE')
{
die("Input a VALID Domain address!");
}
?>


However, I can't seem to get the FILTER_VALIDATE_DOMAIN correct. Can you show me how to validate the domain ?

Thanks
Copy linkTweet thisAlerts:
@developer_webauthorSep 03.2020 — @Nachfolger#1622954

So you are saying these are wrong ...

if (!filter_var("https://www.google.com", FILTER_VALIDATE_URL) = TRUE )



if (filter_var("https://www.google.com", FILTER_VALIDATE_URL) !=TRUE)



if (filter_var("https://www.google.com", FILTER_VALIDATE_URL) !==TRUE)



if (!filter_var("https://www.google.com", FILTER_VALIDATE_URL) ==TRUE)



if (!filter_var("https://www.google.com", FILTER_VALIDATE_URL) ===TRUE)



if (filter_var("https://www.google.com", FILTER_VALIDATE_URL) !===TRUE)


To me, "not equals to" and "not equals" seem the same thing. What is the real difference I have forgotten. When you use one over the other ?

I have forgotten when you use "===" too.


if ($var ===TRUE)
echo "var is set";
Copy linkTweet thisAlerts:
@developer_webauthorSep 03.2020 — @Nachfolger#1622954

So you are saying these are wrong ...

if (!filter_var("https://www.google.com", FILTER_VALIDATE_URL) = TRUE )



if (filter_var("https://www.google.com", FILTER_VALIDATE_URL) !=TRUE)



if (filter_var("https://www.google.com", FILTER_VALIDATE_URL) !==TRUE)



if (!filter_var("https://www.google.com", FILTER_VALIDATE_URL) ==TRUE)



if (!filter_var("https://www.google.com", FILTER_VALIDATE_URL) ===TRUE)



if (filter_var("https://www.google.com", FILTER_VALIDATE_URL) !===TRUE)


To me, "not equals to" and "not equals" seem the same thing. What is the real difference I have forgotten. When you use one over the other ?

I have forgotten when you use "===" too.


if ($var ===TRUE)
echo "var is set";
Copy linkTweet thisAlerts:
@developer_webauthorSep 03.2020 — 
<?php

$submitted_url = $_GET['url'];
$submitted_domain = $_GET['domain'];
$submitted_email = $_GET['email'];

if(!filter_var($submitted_url,FILTER_VALIDATE_URL))
{
die("Input a VALID Url!");
}

if(!filter_var($submitted_email,FILTER_VALIDATE_EMAIL))
{
die("Input a VALID Email!");
}

if(!filter_var($submitted_email,FILTER_VALIDATE_DOMAIN))
{
die("Input a VALID Domain!");
}
?>


I updated it to the above. Are they ok now ?

But do reply to my previous post. Note the "!".

Can't get this one fixed, though ...

if(!filter_var($submitted_email,FILTER_VALIDATE_DOMAIN))
{
die("Input a VALID Domain!");
}
Copy linkTweet thisAlerts:
@NogDogSep 03.2020 — I know, you hate reading the manual, but...

https://www.php.net/filter_var says:

>

Return Values
> -------


> Returns the filtered data, or FALSE if the filter fails.


It either returns a string of some length, or Boolean false. It does not return the string "FALSE" (unless that happens to be the string you're filtering). So the most robust way to check if something is "valid" is probably:
<i>
</i>if(filter_var("https://www.google.com", FILTER_VALIDATE_URL) !== false) {
// it's good
}

If you want to test if it's not valid, then:
<i>
</i>if(filter_var("https://www.google.com", FILTER_VALIDATE_URL) === false) {
// it's bad
}

Using the type-explicit === and !== comparison operators avoids treating empty strings or something like "0" as being false.
Copy linkTweet thisAlerts:
@developer_webauthorSep 03.2020 — @NogDog#1622961

This piece of work seems to e working to INSERT form submission to db.

However, I failed to FILTER or VALIDATE some user inputs. I get error. And so commented-out those lines that give error.

I need help here ....


/*
if(!filter_var($submitted_page_link_anchor_text,FILTER_VALIDATE_STRING)) //Q1. HOW TO VALIDATE THIS ?
{
die("Input a VALID Link Anchor Text!");
}
*/
/*
if(!filter_var($submitted_page_title,FILTER_VALIDATE_STRING)) //Q2. HOW TO VALIDATE THIS ?
{
die("Input a VALID Page Title!");
}
*/
/*
if(!filter_var($submitted_page_description,FILTER_VALIDATE_STRING)) //Q3. HOW TO VALIDATE THIS ?
{
die("Input a VALID Page Description!");
}
*/
/*
if(!filter_var($submitted_page_keyphrase,FILTER_VALIDATE_TEXT)) //Q5. HOW TO VALIDATE THIS ?
{
die("Input a VALID Keyphrase!");
}
*/
/*
if(!filter_var($submitted_page_keywords,FILTER_VALIDATE_STRING)) //Q6. HOW TO VALIDATE THIS ?
{
die("Input a VALID Keywords!");
}
*/


NogDog, can you teach me how to validate and filter these user inputs ?


<form method="POST" name="submit_link" id="submit_link" action=<?php echo $_SERVER['PHP_SELF'];?>>
<legend>
<label for="page_url">Page Url *</label>
<input type="page_url" name="page_url" id="page_url" placeholder="Type here your Webpage Address (Url)..." REQUIRED>
</legend>
<br>
<legend>
<label for="page_link_anchor_text">Page Link Anchor Text *</label>
<input type="text" name="page_link_anchor_text" id="page_link_anchor_text" placeholder="Type here your Link's Anchor Text ..." REQUIRED>
</legend>
<br>
<legend>
<label for="page_title">Page Title *</label>
<input type="text" name="page_title" id="page_title" placeholder="Type here your Page's Title ..." REQUIRED>
</legend>
<label for="page_description">Page Description *</label>
<input type="text" name="page_description" id="page_description" placeholder="Type here your Page's Description ..." REQUIRED>
</legend>
<br>
<legend>
<label for="page_domain">Page Domain *</label>
<input type="domain" name="page_domain" id="page_domain" placeholder="Type here your Page's Domain ..." REQUIRED>
</legend>
<br>
<legend>
<label for="domain_email">Domain Email *</label>
<input type="domain_email" name="domain_email" id="domain_email" placeholder="Type here your Email Address belonging to your Page's Domain ..." REQUIRED>
</legend>
<br>
<legend>
<label for="page_keyphrase">Page Keyphrase *</label>
<textarea name="page_keyphrase" id="page_keyphrase" cols="30" rows="10" placeholder="Type here your Page's Keyphrase ..." REQUIRED>
</textarea>
</legend>
<br>
<legend>
<label for="page_keywords">Page Keywords *</label>
<textarea name="page_keywords" id="page_keywords" cols="30" rows="10" placeholder="Type here your Page's Keywords ..." REQUIRED>
</textarea>
</legend>
<br>
<button name="search" id="search" value=" ">Submit</button>
<br>
<input type="reset">
</form>
Copy linkTweet thisAlerts:
@developer_webauthorSep 10.2020 — @NachFolger,

Can you teach me how to validate STRINGS in user inputs ?

Look at this form. I need to add validations and filters here:


<form method="POST" name="submit_link" id="submit_link" action=<?php echo $_SERVER['PHP_SELF'];?>>
<legend>
<label for="page_url">Page Url *</label>
<input type="page_url" name="page_url" id="page_url" placeholder="Type here your Webpage Address (Url)..." REQUIRED>
</legend>
<br>
<legend>
<label for="page_link_anchor_text">Page Link Anchor Text *</label>
<input type="text" name="page_link_anchor_text" id="page_link_anchor_text" placeholder="Type here your Link's Anchor Text ..." REQUIRED>
</legend>
<br>
<legend>
<label for="page_title">Page Title *</label>
<input type="text" name="page_title" id="page_title" placeholder="Type here your Page's Title ..." REQUIRED>
</legend>
<label for="page_description">Page Description *</label>
<input type="text" name="page_description" id="page_description" placeholder="Type here your Page's Description ..." REQUIRED>
</legend>
<br>
<legend>
<label for="page_domain">Page Domain *</label>
<input type="domain" name="page_domain" id="page_domain" placeholder="Type here your Page's Domain ..." REQUIRED>
</legend>
<br>
<legend>
<label for="domain_email">Domain Email *</label>
<input type="domain_email" name="domain_email" id="domain_email" placeholder="Type here your Email Address belonging to your Page's Domain ..." REQUIRED>
</legend>
<br>
<legend>
<label for="page_keyphrase">Page Keyphrase *</label>
<textarea name="page_keyphrase" id="page_keyphrase" cols="30" rows="10" placeholder="Type here your Page's Keyphrase ..." REQUIRED>
</textarea>
</legend>
<br>
<legend>
<label for="page_keywords">Page Keywords *</label>
<textarea name="page_keywords" id="page_keywords" cols="30" rows="10" placeholder="Type here your Page's Keywords ..." REQUIRED>
</textarea>
</legend>
<br>
<button name="search" id="search" value=" ">Submit</button>
<br>
<input type="reset">
</form>


I tried it like this. The lines that are not commented-out are working fine. the ones I commented-out I am having hiccups:


if(!filter_var($submitted_page_url,FILTER_VALIDATE_URL))
{
die("Input a VALID Page Url!");
}
/*
if(!filter_var($submitted_page_link_anchor_text,FILTER_VALIDATE_STRING)) //Q1. HOW TO VALIDATE THIS ?
{
die("Input a VALID Link Anchor Text!");
}
*/
/*
if(!filter_var($submitted_page_title,FILTER_VALIDATE_STRING)) //Q2. HOW TO VALIDATE THIS ?
{
die("Input a VALID Page Title!");
}
*/
/*
if(!filter_var($submitted_page_description,FILTER_VALIDATE_STRING)) //Q3. HOW TO VALIDATE THIS ?
{
die("Input a VALID Page Description!");
}
*/
if(!filter_var($submitted_page_domain,FILTER_VALIDATE_DOMAIN)) //Q4. HOW TO VALIDATE THIS ?
{
die("Input a VALID Domain!");
}
if(!filter_var($submitted_domain_email,FILTER_VALIDATE_EMAIL))
{
die("Input a VALID Email Address!");
}
/*
if(!filter_var($submitted_page_keyphrase,FILTER_VALIDATE_TEXT)) //Q5. HOW TO VALIDATE THIS ?
{
die("Input a VALID Keyphrase!");
}
*/
/*
if(!filter_var($submitted_page_keywords,FILTER_VALIDATE_STRING)) //Q6. HOW TO VALIDATE THIS ?
{
die("Input a VALID Keywords!");
}
*/
````

NachFolger, on the html, should I use <legend> or <br> ? I want your opinion.

Thanks man. :)
Copy linkTweet thisAlerts:
@NachfolgerSep 10.2020 — @developer_web#1623164

If all you're checking for FILTER_VALIDATE_STRING is if it's a string, just use is_string. Also, FILTER_VALIDATE_TEXT is not a filter.


https://www.php.net/manual/en/filter.filters.validate.php
Copy linkTweet thisAlerts:
@developer_webauthorOct 04.2020 — @Nachfolger#1623165

Aware of that link.I guess need to stick:


FILTER_SANITIZE_STRING



$domain_email = $password = "";

if($_SERVER['REQUEST_METHOD'] == "GET")
{
$domain_email = $_GET['domain_email'];
$password = $_GET['password'];

//STEP 1
if(!filter_var($domain_email,FILTER_SANITIZE_EMAIL))
{
die("Input a VALID Email Address!");
}
if(!filter_var($domain_email,FILTER_VALIDATE_EMAIL))
{
die("Input a VALID Email Address!");
}

if(!filter_var($password,FILTER_SANITIZE_STRING))
{
die("Input a VALID Password!");
}
Copy linkTweet thisAlerts:
@developer_webauthorOct 12.2020 — Folks,

I guess this is wrong way round:



if(!filter_var($domain_email,FILTER_VALIDATE_EMAIL))
{
die("Input a VALID Email Address!");
}

if(!filter_var($domain_email,FILTER_SANITIZE_EMAIL))
{
die("Input a VALID Email Address!");
}


Should be ?



if(!filter_var($domain_email,FILTER_SANITIZE_EMAIL))
{!filter_var($domain_email,FILTER_VALIDATE_EMAIL)
die("Input a VALID Email Address!");
}
if(!filter_var($domain_email,FILTER_VALIDATE_EMAIL))
{
die("Input a VALID Email Address!");
}



Or are these any better ?


if(!filter_var($domain_email,FILTER_VALIDATE_EMAIL) && !filter_var($domain_email,FILTER_SANITIZE_EMAIL))
{
die("Input a VALID Email Address!");
}



if(!filter_var($domain_email,FILTER_VALIDATE_EMAIL) || !filter_var($domain_email,FILTER_SANITIZE_EMAIL))
{
die("Input a VALID Email Address!");
}

Which one will you stick to ?
Copy linkTweet thisAlerts:
@developer_webauthorOct 31.2020 — @NogDog,

Which one would you stick to ?

NogDog, What can I do without ya, hey ?
Copy linkTweet thisAlerts:
@NogDogOct 31.2020 — If you are _validating_ a value to see if you want to accept it or not, use a FILTER_VALIDATE_* filter. It will then return a Boolean false if it's not accepted, otherwise it will return the string.

If you use a FILTER_SANITIZE_* filter, it always just returns a string (with certain things filtered out based on the filter type) -- you do not get a Boolean false if it thinks it's bad.

Warning: even with a validate filter, if it returns "" or "0", they will be treated as false in a simple comparison (e.g. using the ! operator or == or !=. If those values would be valid, then you need to use a type-specific comparison (=== false or !== false, e.g.).
Copy linkTweet thisAlerts:
@developer_webauthorNov 09.2020 — @NogDog

Do you mind chiming in ?
Copy linkTweet thisAlerts:
@developer_webauthorNov 09.2020 — @NogDog#1624748

Can you show me 2 examples where it returns "" or "0" ? That way, it will sink into my subconscious what you trying to teach. Else, I will forget what you said and one day open a thread on same subject.
Copy linkTweet thisAlerts:
@NogDogNov 09.2020 — <i>
</i>11:21 $ /usr/bin/php -a
Interactive shell

php &gt; $foo = '0';
php &gt; $valid = filter_var($foo, FILTER_VALIDATE_EMAIL);
php &gt; var_dump($valid);
bool(false) // not a valid email, so it returns an actual FALSE
php &gt; $valid = filter_var($foo, FILTER_SANITIZE_EMAIL);
php &gt; var_dump($valid);
string(1) "0" // returned the string, not a Boolean
php &gt; if($valid == false) {
php { echo "0 treated as false"; } // 0 is "falsey" in a loose comparison
0 treated as false
php &gt; if($valid === false) { // 0 is not false in an "exact" comparison
php { echo "0 identtical to false";} else { echo "0 not identical to false"; }
0 not identical to false
Copy linkTweet thisAlerts:
@kingsizexxNov 10.2020 — thanks https://websitestrategies.com.au/seo-training/
Copy linkTweet thisAlerts:
@developer_webauthorNov 19.2020 — @NogDog#1624942

Thanks.

I don't understand oop. Nevertheless, understood your point.

I always forget or really didn't the difference between "==" and "===".

I didn't know you can use "==" for fuzzy match.

Anyway NogDog, care to show me 2 more examples using the "==" for fuzzy match (php example and no sql example) and fuzzy search (sql example) ?

And likewise, 2 more examples using the "===" for exact match (php example and no sql example) and exact search (sql example) ?
Copy linkTweet thisAlerts:
@developer_webauthorDec 03.2020 — @Sempervium

I guess I'd rather stick to samples from you instead. What you say to my previous post ?
×

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 3.29,
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: @darkwebsites540,
tipped: article
amount: 10 SATS,

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

tipper: Anonymous,
tipped: article
amount: 10 SATS,
)...