Braxton Posted August 3, 2012 Share Posted August 3, 2012 I am currently writing a simple blog script for my website and I have reached the point where I am writing in the commenting feature. I want to allow the author of a comment to also provide their url much like how wordpress comments work. The thing that I'm unsure about is the url. Some users like to simply type domain.com and other http://domain.com. My question is, is there a function or method of which would be good to use here to make sure that when a user submits the info, the url won't break or anything like that. I hope this makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/266660-handling-user-submitted-urls/ Share on other sites More sharing options...
requinix Posted August 4, 2012 Share Posted August 4, 2012 URL won't break? How would it break in the first place? Quote Link to comment https://forums.phpfreaks.com/topic/266660-handling-user-submitted-urls/#findComment-1366700 Share on other sites More sharing options...
Braxton Posted August 4, 2012 Author Share Posted August 4, 2012 Let's say the user simply supplies "domain.com", Should I just check for http:// within the string and if it does not exist within, then add it? I just tested using "domain.com" and when I click on it, I am taken to "http://localhost/domain.com" I used the term "break" as I didn't know how else to say it. Quote Link to comment https://forums.phpfreaks.com/topic/266660-handling-user-submitted-urls/#findComment-1366706 Share on other sites More sharing options...
requinix Posted August 4, 2012 Share Posted August 4, 2012 Are you modifying existing WordPress code or writing something for yourself? Quote Link to comment https://forums.phpfreaks.com/topic/266660-handling-user-submitted-urls/#findComment-1366715 Share on other sites More sharing options...
DavidAM Posted August 4, 2012 Share Posted August 4, 2012 You got the "localhost/" on it because without the leading slash (on "domain.com") the browser thinks it is a relative path, and automatically applies the path of the current page. At the very least, you need a slash at the beginning: /domain.com. However, I would instruct the user to include the protocol ("http:") and reject the submission if it is not present. It is possible that the user's protocol is https:, so you really want to have them enter it, rather than guess. I would resist storing or showing it without the protocol, because if you show it through some other protocol (i.e. rss or some future protocol), you don't really know what that (future) reader will do to unqualified addresses. Quote Link to comment https://forums.phpfreaks.com/topic/266660-handling-user-submitted-urls/#findComment-1366718 Share on other sites More sharing options...
handlegeek Posted August 4, 2012 Share Posted August 4, 2012 Yes, you can use PHP to check if the URL starts with "http://" or "https://" and show error message OR prefix "http://" considering http:// to be default. In addition to that, using JavaScript you can validate the URL field - To check if it follows a valid URL pattern and show a error message to the user. Quote Link to comment https://forums.phpfreaks.com/topic/266660-handling-user-submitted-urls/#findComment-1366719 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.