Url Encoding - Use rawurlencode not urlencode !

The URL encoding is one the mysterious functions that PHP programmers does use but without knowing all the details behind it. When one wants to create a link dynamically he just use urlencode, and thi is so necessary because in an URl, there is some reserved characters that have sens for the browsers and the the server-side scripts. You should already know the meaning of ? , & and = characters in the query string, if you don't know see this important Wikipedia article http://en.wikipedia.org/wiki/Query_string

But, you may see somewhere that there is a function called rawurlencode, what is that ? Is it a different encoding convention ? The answer is yes and not.
Well, urlencode and rawurlencode are very similar, they do the same encoding except for one character, the Space character !
rawurlencode encodes the space character as %20, while urlencode encodes it as +, here are few use-difference between both, as much as I understand:

1-Path vs Query vars
In the url path, you should encode spaces as %20 and not +, for example if your folder is called "folder one"
this url will work http://www.exemple.com/folder%20one/index.php , and this will not : http://www.exemple.com/folder+one/index.php because in this last the server will search for a folder called folder+one instead of folder one .
But in the GET variables you should prefer the + character to simulate the space, this is what Google is doing ! But whether you use + or %20 in those url variables, ie you use urlencode or rawurlencode, they should both work.

2-PHP vs JavaScript:
Javascript uses the same convention as rawurlencode, so if you want to encode an url with php but you will decode it in javascript, you should use rawurlencode, and here is the reason:

Lets say you encoded "test 1" within php, if you need to "decode" it in JavaScript using decodeURI() function then decodeURI("test+1") will give you "test+1" while decodeURI("test%201") will give you "test 1" as result.

The rawurlencode function is the last one, and it should work perfectly in modern systems with less problem. So this is the encoding function to use most of the time ! While urlencode is kept for legacy compatibility, as well as compatibility with Javascript.


d3 gold said...

Well, to the gossip put off, we get down to business!

LOL elo boosting said...

not just is the idea very amazing, however it is really nicely performed -- i really like all your sketches, they're therefore stunning to check out! contributing to my personal book marks.

elo boost

Cheap Fifa 14 Coins

Fully Help said...

Checkout Great beginning php tutorials Very clear and helpful for beginners.

Priya Tamil said...

Your posts is really helpful for me.Thanks for your wonderful post.It is really very helpful for us and I have gathered some important information from this blog.

SEO Training in Chennai

Dhiya L said...

Thanks for sharing the valuable information here. So i think i got some useful information with this content. Thank you and please keep update like this informative details.

Dotnet Training in Chennai

sindhu said...

Good post. I learn something totally new and challenging on blogs I stumble upon on a daily basis. It will always be interesting to read articles from other authors and practice something from their websites...

CRO Agency in Chennai

Shalini said...

That was a great message in my carrier, and It's wonderful commands like mind relaxes with understand words of knowledge by information's.

digital marketing company in chennai
seo company in chennai

Samsun cilingir said...

Selam yonetici admin siteniz cok guzel basarilarinizin devamini bekleriz

Vignesh PV said...

This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.
PHP Training in Chennai

Anil Gupta said...

Thank you for posting such nice article read. I am very glad to read this. Really good one.
Web Designing Company in Bangalore, Web Development Company Bangalore