Mobile app version of vmapp.org
Login or Join
Chiappetta492

: URLEncode of full URL I am using the PHP urlencode() function to escape chars for anchor tags (). Why can't I use the function on a full URL such as http://www.allposters.com/gallery.asp?startat=/getthumb.asp&txtSearch=

@Chiappetta492

Posted in: #Php #UrlEncoding

I am using the PHP urlencode() function to escape chars for anchor tags ().
Why can't I use the function on a full URL such as
www.allposters.com/gallery.asp?startat=/getthumb.asp&txtSearch= ...


The function converts this string to

http%3A%2F%2Fwww.allposters.com%2Fgallery.asp%3Fstartat%3D%2Fgetthumb.asp%26txtSearch


which does not work when clicking on...

Trying to escape the QUERY only

startat%3D%2Fgetthumb.asp%26txtSearch


breaks the link also.

So my question is which characters should I esacape using the urlencode() function?

Thanks,

Joel

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Chiappetta492

1 Comments

Sorted by latest first Latest Oldest Best

 

@Angela700

URLEncode is meant to encode characters that have special meanings in URLs (like colons, question marks, slashes etc.).

You should only apply URLEncode to the values you wish to encode in your URL, not to the entire URL.

E.g. Suppose I have the following URL:
example.com/search.php?query=VALUE

As it stands, no escaping is necessary. But suppose VALUE was actually

/. the book


The VALUE bit now contains a character that is not safe as a slash has special meaning in an URL.

So before I attach the VALUE to the URL, I need to URLEncode just the VALUE.
example.com/search.php?query=%2F.%20the%20book

When a webserver gets the above request it will need to URLDecode the query parameter to make get the original text back.

In the example cited in the question, the entire query string was being encoded. This is incorrect as special characters (such as ampersands between query parameters) are then escaped and not interpreted correctly. You should only encode the parameter values.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme