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.
filter_var URL validation always returning same result.
Re: filter_var URL validation always returning same result.
You seem to be using it right, what is the URL you are entering ?
Re: filter_var URL validation always returning same result.
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.
Do you get "Success" if you enter an invalid url?
Re: filter_var URL validation always returning same result.
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.
[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.
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.
Re: filter_var URL validation always returning same result.
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.
Re: filter_var URL validation always returning same result.
Perhaps the problem was with the definition of $url ?
Re: filter_var URL validation always returning same result.
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