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.

Summary:
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.

14 comments:

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

SARA MARK said...

This is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article.
Language Translation Services ,
Subtitling Companies in Bangalore

cyber news said...

thanks for sharing the information.Indian Cyber Army is announcing “ Summer Internship 2018” on “ Ethical hacking and Cyber Crime Investigation” for the enthusiasts of Cyber security. Here internship will give you on-the-job experience, help you learn whether you and Cyber security industry are a good match and can provide you with valuable connections and references. Here interns are usually exposed to a wide variety of tasks and responsibilities which allows the intern to showcase their strengths by working on projects for various managers that work on different parts of Indian Cyber Army. Becoming a high performing intern is a fantastic way to improve your employment prospects. This internship can be a great way to get your foot in the door of success with a prestigious or desirable Organization

cyber news said...

thanks for sharing the more valuable information.
Indian Cyber Army credibility in Ethical hacking training & Cybercrime investigation training is acknowledged across nation as we offer hands on practical knowledge and full assistance with basic as well as advanced level ethical hacking & cybercrime investigation courses. The training is conducted by subject specialist corporate professionals with wide experience in managing real-time ethical hacking/ cyber security projects. Indian Cyber Army implements a blend of academic learning and practical sessions to give the candidate optimum exposure.Ethical hacking training ,
Ethical hacking course

preeti verma said...

Top MCA

colleges in greater noida

Top MSC IT

colleges in greater noida

Top M

TECH colleges in greater noida