Link to home
Start Free TrialLog in
Avatar of ie7
ie7Flag for United States of America

asked on

urlencode url encode

how does urlencode work.
does it change the url in the browser

difference between urldecode
and differences between javascript and php for urlencode, urldecode
Avatar of christophermccann
christophermccann
Flag of United Kingdom of Great Britain and Northern Ireland image

urlencode converts a string to one that can be used as a url. That means it changes characters such as spaces to + signs.

Generally I use it if i am putting a string into a url query i.e. www.domain-name.com?query=<?php echo urlencode($string); ?>

Does this help?
Avatar of Ionut A. Tudor
What is the question ? the urlencode() function just encodes the data so that it could be sent over via the HTTP protocol, for example whitespaces or non-alphanumeric char would be encoded so that your data won't break and the server could understand the data that's being received correctly.
In answer to your other questions urlencode does not change the url in the browser only adapts the string you pass to it as a parameter.

urldecode() converts the string back to the original.

There is no exact equivalent function in javascript however you can use  encodeURIComponent() but that does not deal with exactly the same characters as PHP -  namely it does not escape the following characters:     ! ~ * ' ( )
Avatar of wuff1uk
wuff1uk

The function does not change the url in the browser and it doesn't redirect either. Urlencode is a function which replaces special characters of a string for use in urls, in order for them not to interfere with query strings or the url itself.

I.e. if you have user input like
"Do I want to know about this & that?" and want to use it in an url the &, the ? would interfere with the URL standard, so you need to convert it in an URL friendly way.
urlencode transforms the sentence into this:
Do+I+want+to+know+about+this+%26+that%3F

The usage would be:
urlencode($string) or urlencode("this is the test string of this & that")

The return value of the function is the encoded string and can be assigned to variables or embedded.

More info here: http://uk3.php.net/urlencode

Javascript does not have a urlencode function by itself, but an escape function which is similar but not the same. A comparison can be found here:
http://cass-hacks.com/articles/discussion/js_url_encode_decode/
However, there are functions written which you could include to copy the php urlencode function when necessary. Like here http://phpjs.org/functions/urlencode:573
The JavaScript equivalent is function escape(), you can read more here: http://www.w3schools.com/jsref/jsref_escape.asp
And to be more specific, every programming language be it client side or server side will encode any non-alphanumeric and whitespaces chars because they are all sent via HTTP protocol and the data needs to be valid or it will break.
 
Sorry to disagree but escape is not the same. Using escape in place of urlencode exposes a vulnerability. See here: http://cass-hacks.com/articles/discussion/js_url_encode_decode/
@christophermccann, i stand corrected, however unescape does work properly so the + * / that aren't encoded correctly by escape are being decoded with their coresponding codes correctly using unescape. Thanks for this info, didn't encountered it yet in practice but its good to know.
Cheers
Avatar of ie7

ASKER

urlencode
is it used as a link   (href)
or does it automatically change the text in the url text box in the browser
ASKER CERTIFIED SOLUTION
Avatar of christophermccann
christophermccann
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ie7

ASKER

explained well, thank you