Page 1 of 1

filter_var URL validation always returning same result.

Posted: Thu Oct 13, 2011 1:38 am
by Temor
Hi guys. I'm trying to validate a URL using this code here:
if(filter_var($url, FILTER_VALIDATE_URL) === false){
		echo "Success";}
	else{
		echo "Failure";}
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.

Re: filter_var URL validation always returning same result.

Posted: Thu Oct 13, 2011 3:22 pm
by jacek
You seem to be using it right, what is the URL you are entering ?

Re: filter_var URL validation always returning same result.

Posted: Thu Oct 13, 2011 3:33 pm
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.

Re: filter_var URL validation always returning same result.

Posted: Thu Oct 13, 2011 3:45 pm
by EcazS
Do you get "Success" if you enter an invalid url?

Re: filter_var URL validation always returning same result.

Posted: Thu Oct 13, 2011 4:24 pm
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 :)

Re: filter_var URL validation always returning same result.

Posted: Thu Oct 13, 2011 4:41 pm
by EcazS
if(filter_var($url, FILTER_VALIDATE_URL)){
                echo "Success";}
        else{
                echo "Failure";}
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.

Re: filter_var URL validation always returning same result.

Posted: Thu Oct 13, 2011 5:43 pm
by Temor
EcazS wrote:
if(filter_var($url, FILTER_VALIDATE_URL)){
                echo "Success";}
        else{
                echo "Failure";}
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.

Re: filter_var URL validation always returning same result.

Posted: Thu Oct 13, 2011 11:18 pm
by jacek
Perhaps the problem was with the definition of $url ?

Re: filter_var URL validation always returning same result.

Posted: Fri Oct 14, 2011 12:47 am
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 :)