filter_var URL validation always returning same result.

Ask about a PHP problem here.
Post Reply
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

filter_var URL validation always returning same result.

Post by Temor »

Hi guys. I'm trying to validate a URL using this code here:
[syntax=php]if(filter_var($url, FILTER_VALIDATE_URL) === false){
echo "Success";}
else{
echo "Failure";}[/syntax]

My problem here is that it always echoes "Failure" even if my URL is valid.

I might not have understood what filter_var really does, so correct me if I'm wrong, but if it filters the URL ( i.e the URL is invalid ) it returns the filtered URL. If it does not filter it ( i.e the URL is valid ) it returns false.

Can anyone tell me what I'm doing wrong?. Any input is appreciated.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: filter_var URL validation always returning same result.

Post by jacek »

You seem to be using it right, what is the URL you are entering ?
Image
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: filter_var URL validation always returning same result.

Post by Temor »

jacek wrote:You seem to be using it right, what is the URL you are entering ?

http://betterphp.co.uk

I've been trying every possible comination of it.

just betterphp.co.uk, www.betterphp.co.uk, http://www.betterphp.co.uk.

I've also tried abc123 and a faceroll. Still got the same result each time.
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: filter_var URL validation always returning same result.

Post by EcazS »

Do you get "Success" if you enter an invalid url?
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: filter_var URL validation always returning same result.

Post by Temor »

EcazS wrote:Do you get "Success" if you enter an invalid url?

I seem to have fixed it now... I just rewrote it and it started giving me different values for different urls.

I must have been mistaking on how it outputs though, because an invalid url gives me a success message and a valid url gives me a failure message.

I'll just turn it all around and retry. Will post again if I run into more problems :)
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: filter_var URL validation always returning same result.

Post by EcazS »

[syntax=php]
if(filter_var($url, FILTER_VALIDATE_URL)){
echo "Success";}
else{
echo "Failure";}
[/syntax]

Should work though. I've heard that filter_var is false by default but I think it's true by default. At least that's how mine worked.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: filter_var URL validation always returning same result.

Post by Temor »

EcazS wrote:[syntax=php]
if(filter_var($url, FILTER_VALIDATE_URL)){
echo "Success";}
else{
echo "Failure";}
[/syntax]

Should work though. I've heard that filter_var is false by default but I think it's true by default. At least that's how mine worked.

Alright, I'll try it out, thanks.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: filter_var URL validation always returning same result.

Post by jacek »

Perhaps the problem was with the definition of $url ?
Image
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: filter_var URL validation always returning same result.

Post by Temor »

jacek wrote:Perhaps the problem was with the definition of $url ?

It's possible I had a typo somewhere.
I only had like 8 lines of code so it didn't take long to rewrite it and it magically solved it :)
Post Reply