Why URL's need encoding?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Why URL's need encoding?

Post by raghavan20 »

I dont really understand the importance of URL encoding? why is it done?
I understand the way its done but want to know why we have to do it and where it should be done?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it helps avoid errors in the client's processing..

full of questions today, aren't we?
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

jus developed a few applications; everything seem to be working fine until now. but I want to make sure I develop upto standards and use the best way when things can be done in different ways so I had to clear basic doubts atleast by now.

it helps avoid errors in the client's processing..
what do you mean by errors can come up during client's processing?
can you give me an example if you dont mind?
do we have to this when we normally pass links like

Code: Select all

echo "viewPost.php?action=viewPost&id=3";
do you say that I have to encode the above url in the php statement?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

certain parts require encoding when they contain non-standard characters, such as spaces, slashes, various other characters that may be accidentally thought as components of url's...

only the components should be processed... such as individual directory names, the filename, the keys and values of the query string, and possibly the hash..

Your example doesn't require it as it does not contain any mistakable characters.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

raghavan20 wrote:do you say that I have to encode the above url in the php statement?
"viewPost" is OK because it contains only alphanumeric characters. If you were passing an unknown string via GET - such as a search term - it might contain anything and so you'd need to encode.
Last edited by McGruff on Mon Aug 08, 2005 3:16 pm, edited 1 time in total.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

You should however replace & with &

This is to comply with the new xhtml standards, notice how when you place a copyright symbol, or a bullet in html you have to go &xxx; where xxx is an alphanumerica value, for example:
" for a quote

well when you output & in a url the clients web browser has to check wether or not you're trying to output a special html code.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

jshpro2 wrote:You should however replace & with &

This is to comply with the new xhtml standards
Thats an html standard, and its not new. It simply didn't cause user-agents to break until xhtml.

http://www.w3.org/TR/1999/REC-html401-1 ... ml#h-5.3.2

Its been around for 6 YEARS. :)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Ahh, I didn't hear about it untill I started converting my pages to xhtml which caused me to think that... yeah...
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Imagine you want a user to submit a URL to your website, and your form is set to use GET.

The user submits:

http://www.ooer.com/index.php?section=php&id=2&page=2

Your url will end up being:

http://www.example.com/form.php?url=htt ... d=2&page=2

How would a anything reading that URL know where one begins and the other ends? By url encoding the GET request bit everythign is made clear.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

onion2k wrote:Imagine you want a user to submit a URL to your website, and your form is set to use GET.

The user submits:

http://www.ooer.com/index.php?section=php&id=2&page=2

Your url will end up being:

http://www.example.com/form.php?url=htt ... d=2&page=2

How would a anything reading that URL know where one begins and the other ends? By url encoding the GET request bit everythign is made clear.
Alright!!! But if I gonna get a website link from a user. Then the user types in some link like the above you mentioned. Then if he clicks on the submit button it automatically gets appended to the url. I dont have a chance to really encode the url.
How do I do that, but I have a slight feeling that when variables are submitted through a form it automatically gets encoded??? :(
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Yes the browser (user-agent) will urlencode forms. However, if you're manually creating and GET-style URLs for navigation, etc, you should urlencode them yourself.
Post Reply