Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/266660-handling-user-submitted-urls/
Share on other sites

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.

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.