Jump to content

URLs in Input Boxes


raku

Recommended Posts

Hey,

 

I am trying to allow users to enter links into an input box from the URL, for example: http://example.com/?url=http://google.com

 

But, when these URLs have spaces in them, they are shown in the input box as "http://test.com/test test" instead of "http://test.com/test%20test".

 

I have used htmlentities and strip_tags to make sure their inputs are safe, but how can I keep the special characters from being converted (like %20 to space)?

 

Thanks!

Link to comment
Share on other sites

That converts "http://" as well. Should I trim it off before using urlencode, then add it back on?

 

Also, if I'm adding this into a database and displaying it to other users, do I need to do anything more than htmlentities, strip_tags, and mysql_real_escape_string? There are some other text field inputs as well that I'm using those functions for.

 

Thanks.

Link to comment
Share on other sites

Thanks!

 

Could you please reply to my urlencode question? Should I trim off the "http://" before using the function?

 

Also, users can add descriptions or comments for the URLs. Is it okay to only apply strip_tags to them, or do I need to use htmlentities as well? I'm guessing that I should use htmlentities if they are carried in the link as a variable.

 

Link to comment
Share on other sites

urlencode() should only be used for the query string part of a URL, but yes, you should be able to split the URL into two parts, and then use it:

 

<?php
$url = 'http://test.com/test test';
$parts = explode('/', $url);
$end = urlencode(array_pop($parts));
$url = implode('/', $parts) . '/' . $end;
echo $url;
// http://test.com/test+test
?>

 

This will only encode the string after the last slash. Not sure it's what you're looking for..

Link to comment
Share on other sites

Awesome, thanks so much!

 

Could you please comment on htmlentities and strip_tags? If I'm accepting content through the URL like that, should I use both functions in addition to mysql_real_escape_string when I enter it in to my database? I also output it to users at times, and would like it to look appropriate while being safe.

 

Thanks again!

 

Edit:

That script doesn't work in a some situations:

If the URL is http://test.com/test%20test, it modifies it, but I think it's fine the way it is. They are entering URLs from websites that exist, and lots of times they put the title in the html name like.. http://example.com/blog/php%20is%20awesome.html

 

Also, it doesn't work on variables on the end of the string or if there are multiple slashes.

Link to comment
Share on other sites

Thanks for your help guys.

 

I think the solution is to require users to enter an encoded url, otherwise the input boxes won't be filled out properly. It seems to work when the url is entered like:

 

http://example.com/testing?url=http%3A%2F%2Fwww%2Esample%2Ecom%2Fblogs%2F

 

For security would I need to use anything more than strip_tags and escaping on the URL string?

Link to comment
Share on other sites

Yes, that would work.

 

As for the security, I'm really not sure how to handle it. If you want to serve the entered URL as a clickable working URL, it would have to be urldecode()'ed. Maybe urldecode() it, then strip_tags() it for potential urlencoded HTML, and then it's good to go?

 

.. Too tired, gonna hit the bed now ;)

Link to comment
Share on other sites

Thanks again.

 

Without using urldecode(), the URL appears correctly in the input box. http%3A%2F%2Fwww%2Esample%2Ecom%2Fblogs%2F appears as http://www.sample.com/blogs/

 

I'm guessing I don't need urldecode?

 

I don't allow any HTML in any of these fields, it's just URL and a comment. I'm using strip_tags to get rid of any formatting they might try and use, and htmlentities to try and prevent any other kind of malicious stuff.

Link to comment
Share on other sites

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.