Jump to content
Larry Ullman's Book Forums

Geting Url Form Users And Validate Them


Recommended Posts

Hello...

 

I have a form to get some urls from users. Eg: Web Address, Facebook Address, Twitter Address, Google+ address etc... My problem is how I validate these urls when they submit the form. I tried to validate URL in PHP by using the FILTER_VALIDATE_URL or simply, using regular expression.

 

Here, I would like to know what are the best methods to get such a urls from users. Is it always good to let them to enter protocol? sometimes they may not know it is http, https, ftp, ftps.. etc. I think it is something hard to do some users.

 

I tried something like this using FILTER_VALIDATE_URL, But it always use protocol and sometime I am confusing how its work..

 

// validate url
$url = '[url="http://www.example.com%27;"]http://www.example.com';[/url]

if (filter_var( $url, FILTER_VALIDATE_URL)){
echo "<br>valid";
} else {
echo "<br>invalid";
}

[b]OUTPUT[/b] : valid

 

 

// validate url
$url = 'hp://www.example.com';

if (filter_var( $url, FILTER_VALIDATE_URL)){
echo "<br>valid";
} else {
echo "<br>invalid";
}

[b]OUTPUT[/b] : valid

 

// validate url
$url = 'http://example.com';

if (filter_var( $url, FILTER_VALIDATE_URL)){
       echo "<br>valid";
} else {
       echo "<br>invalid";
}

[b]OUTPUT[/b]: valid

 

// validate url
$url = 'http://example.com?id=32&name=kamalani';

if (filter_var( $url, FILTER_VALIDATE_URL)){
       echo "<br>valid";
} else {
       echo "<br>invalid";
}

[b]OUTPUT[/b] : valid

 

Can you tell me what are the best ways to get urls from user and how those validate?

Any comments are greatly appreciating..

 

Thank you.

Link to comment
Share on other sites

It would be worth investing your time in learning the Yii Framework it has built in validators like url which will quickly valid a URL and which have been thoroughly tested. Yii will help you to be able to build more bug free professional websites and with 3 times the speed. Which also means more money for you as a web developer as you can get more projects done. :P

Link to comment
Share on other sites

Edward, a little bit less of a Yii sales pitch would have been nice.

 

thara, you said you tried to use FILTER_VALIDATE_URL in PHP. Did it work? It should.

I would use FILTER_VALIDATE_URL, if possible, and as you suggested, I might omit the protocol entry part, as all links should probably be either http or https.

  • Upvote 3
Link to comment
Share on other sites

What's the purpose here? It's important, as absolutely validating an URL may be tricky. Is it enough to be reasonably sure that the URL is correct and make sure the URL is safe to display? Then a mix of the FILTER_VALIDATE_URL filter and htmlentities() might be enough for you.

 

Remember to fit your solution to your needs. After all, security and usability might be the most important here. Once a URL is safe, and you can reasonably assume it has the correct structure, you might have done enough.

  • Upvote 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...