When i'm trying to put russian text in cookie via javascript and then output it via php it returns:
%u043F%u0440%u043E%u0432%u0435%u0440%u043A%u0430
How to decode this to normal cyrillic characters?
This is the function i'm using to pass to document.cookie:
function setCookie(c_name,val,c_expiredays,c_path,c_domain,c_secure)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+c_expiredays);
document.cookie=c_name+ "=" +escape(val)+
/* Additional settings */
((c_path) ? "; path=" + c_path : "") +
((c_domain) ? "; domain=" + c_domain : "") + // used to allow using only on a certain domain
((c_secure) ? "; secure" : "") + // used for HTTPS (SSL)
((c_expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
setCookie('name',$(this).val(),1);
On server side, i'm outputting like that:
(isset($_COOKIE['img_href_value']) ? $_COOKIE['img_href_value'] : '')
You can't put non-ASCII characters directly in a cookie (what happens if you try varies over each different browser and many of them mangles the results irretrievably).
So you have to choose some encoding to use to use on cookie values. It doesn't matter what, as long as your client-side JavaScript and server-side PHP agree on one encoding. URL-encoding is certainly a popular choice of encoding scheme for this purpose, but it's not mandated by any standard and tools won't automatically decode it for you.
To get characters out of a URL-encoded cookie value you must manually call rawurldecode on the value at the PHP end, or decodeURIComponent to extract from document.cookie at the JavaScript end.
To encode to this format the corresponding functions are rawurlencode on the PHP side and encodeURIComponent in JavaScript.
(This assumes you are using UTF-8 for your strings, which you should be.)
Don't use urlencode for this in PHP (it's for form submission parameters only and gets the space character wrong in this context), and definitely don't use escape in JavaScript (it gets every non-ASCII character wrong, coming up with that weird non-standard %uNNNN format you quoted).
(In general, JS escape/unescape is an ancient and highly questionable encoding scheme that you should almost never have any reason to use.)
Try with this: http://kobesearch.cpan.org/htdocs/Encode-JavaScript-Cyrillic/Encode/JavaScript/Cyrillic.pm.html
Related
So from my understanding of PHP and cookies, if I use the setcookie() function, then I get a cookie that is automatically url encoded. And when I go to the $_COOKIE array, I should get the cookie back, automatically url decoded. Problem is, it seems to be decoding the cookie twice when I look in $_COOKIE.
Say I have a cookie whose value is "Name|ID|Email", for example:
Joe|123|my+email#somewhere.com
This would be encoded as:
Joe%7C123%7Cmy%2Bemail%40somewhere.com
Notice the plus sign is encoded, so theoretically I ought to get it back if I decode it. Since this is automatically done in $_COOKIE, I ought to get back what I started with. But instead, I'm getting back:
Joe|123|my email#somewhere.com
Notice the space where the plus used to be. This is what I would expect if I ran an additional urldecode() on the cookie. But I'm not, so I have no idea why I would be getting a space instead of a plus.
Another interesting twist. A refresh on the page seems to produce the correct output. Any ideas why it's behaving like this?
FYI, to set the initial cookie, I use javascript and escape() the script to produce the encoded string. Might this be an hand off issue between javascript and PHP?
Thoughts would be appreciated.
It's worth noting that both "%20" and "+" are valid encodings of a space character. Per the Wikipedia article on URL encoding (emphasis added):
When data that has been entered into HTML forms is submitted, the form
field names and values are encoded and sent to the server in an HTTP
request message using method GET or POST, or, historically, via email.
The encoding used by default is based on a very early version of the
general URI percent-encoding rules, with a number of modifications
such as newline normalization and replacing spaces with "+" instead of
"%20". The MIME type of data encoded this way is
application/x-www-form-urlencoded, and it is currently defined (still
in a very outdated manner) in the HTML and XForms specifications.
More specifically related to PHP and JavaScript, see the top answer on this question:
When to encode space to plus (+) or %20?
Firstly, PHP will always run before JavaScript - it's server side rather than client side so the cookie you set with JavaScript won't actually be available to PHP until you refresh the page (hence that issue).
Next JavaScript has different ways to encode the strings; only one will work with PHP automatically.
So:
document.cookie = "testuser=" + "Joe|123|my+email#somewhere.com";
// Joe|123|my email#somewhere.com (when decoded by PHP)
document.cookie = "testuser=" + escape("Joe|123|my+email#somewhere.com");
// Joe|123|my email#somewhere.com (when decoded by PHP)
document.cookie = "testuser=" + encodeURI("Joe|123|my+email#somewhere.com");
// Joe|123|my email#somewhere.com (when decoded by PHP)
document.cookie = "testuser=" + encodeURIComponent("Joe|123|my+email#somewhere.com");
// Joe|123|my+email#somewhere.com
So, try this for the sake of a test (remember you'll need to refresh the page to see the cookie value):
<html>
<head>
<title>Cookie Juggling</title>
<script type="text/javascript">
document.cookie = "testuser=" + encodeURIComponent("Joe|123|my+email#somewhere.com");
</script>
</head>
<body>
<div><?php echo !empty($_COOKIE['testuser']) ? $_COOKIE['testuser'] : "Cookie not set yet"; ?></div>
</body>
</html>
If you don't want to automatically encode the cookie, you can use setrawcookie function.
The exception with this function is, you can not use these characters: (,; \t\r\n\013\014) :
setrawcookie("NAME","Joe|123|my+email#somewhere.com");
# Output in browser:
Joe|123|my+email#somewhere.com
# Output in PHP `echo $_COOKIE['NAME']`:
Joe|123|my email#somewhere.com
Tested with PHP 5.3
setcookie("NAME","Joe|123|my+email#somewhere.com");
# Output in browser:
Joe%7C123%7Cmy%2Bemail%40somewhere.com
# Output in PHP echo $_COOKIE['NAME']`:
Joe|123|my+email#somewhere.com
now : As an alternative way, you can use setcookie(), and rawurldecode() to decode it:
echo rawurldecode($_COOKIE['NAME'])
my websites sometimes needs to read a javascript cookie using php but sometimes I get weird character set from some users like this `#16 3CFJ) DD%J,'1 while for some users it reads it properly. therefore, I think the problem is in the client-side. I use this method to write cookies:
var expireDate = new Date();
expireDate.setMonth(expireDate.getMonth() + 1);
var value="Sami";
document.cookie = "name="+value+";path=/;expires="+expireDate.toGMTString();
and this $_COOKIE['name']to read it using php.
Cookies cant be handled using headers. So,
Encode your cookie using base64_encode() and decode it using base64_decode() to read it.
To encode/decode in Javascript, this answer might help.
I have a javascript which sends some specific information to a PHP api . Before to send it performs encodeURI . How can I "decode" it in PHP ? I understand that urldecode/urlencode is different that javascript encode/decodeURI so what can I use ?
Use encodeURIComponent in Javascript: http://www.w3schools.com/jsref/jsref_encodeuricomponent.asp and urldecode in PHP: http://php.net/manual/en/function.urldecode.php
Unless you've encoded it multiple times (e.g. by explicitly calling the encode method AND inserting the value into a form field which is then submitted) you don't need to do anything - it is transparently converted back to its original form when the request is parsed.
You can use rawurldecode function in php, but this function is not UTF-8, then your have to convert to UTF-8 with utf8_decode like this
echo utf8_decode(rawurldecode('Educa%C3%A7%C3%A3o%20Multim%C3%ADdia'));
I am trying to use POST in Flash (ActionScript 2), to POST values to PHP mail script.
I tried the PHP mail script with HTML form, and it worked perfectly fine.
But when I POST from flash and input non-English characters, I get "????" in the mail.
I tried utf8_encode($_POST["name"]), but it doesn't help.
Edit:
I also tried utf8_decode($_POST["name"]), it didn't work.
Update: (So you wont have to go through all the comments)
I checked the variables in Flash,
the values are stored correctly.
The HTML page where the Flash is embedded is UTF-8 encoded.
I watched the POST headers with FireBug, the POST itself is already messed up, showing "????" instead of the real value.
The the messed up "????" value, is currently url-encoded by flash, and decoded by PHP, resulting in $_POST["name"] == "???";
I suspect its the sendAndLoad method that creates the mess.
Update:
Here is the flash code:
System.useCodepage = true;
send_btn.onRelease = function() {
my_vars = new LoadVars();
my_vars.email = email_box.text;
my_vars.name = name_box.text;
my_vars.family_box = comment.text;
my_vars.phone = phone_box.text;
if (my_vars.email != "" and my_vars.name != "") {
my_vars.sendAndLoad("http://aram.co.il/ido/sendMail.php", my_vars, "POST");
gotoAndStop(2);
} else {
error_clip.gotoAndPlay(2);
}
my_vars.onLoad = function() {
gotoAndStop(3);
};
};
email_box.onSetFocus = name_box.onSetFocus=message_box.onSetFocus=function () {
if (error_clip._currentframe != 1) {
error_clip.gotoAndPlay(6);
}
};
Flash uses UTF8-encoding for all strings, anyway. If you use LoadVars, transfer as a urlencoded string should also work automatically.
So your problem is most probably in the PHP part of your application. For example, in order for UTF8 to work correctly, all individual PHP files must be saved in UTF8-encoded format, as well.
If just changing the file encoding doesn't work, try parsing $HTTP_RAW_POST_DATA first, check if all the fields have been transferred correctly, then go on and echo your way through until you find the place where the encoding is lost.
Update:
Here is your problem: You use System.useCodePage = true;. This requires you to specifically encode all your data as unicode before sending it. Unless you have any other documents in other encodings, and/or allow your users to upload their own text data with their localized encodings, set System.useCodePage = false;, and your utf8-problem should go away.
If you receive data from flash you need to use utf8_decode and not utf8_encode.
Flash uses UTF8 - as long as you don't tell it to use the local characterset. And you want PHP to decode that to good old ISO-8859-1 which PHP uses internally.
You'd only use utf8_encode when preparing data for flash.
So I know that my server on real form submit turns %CE%EB%E5%E3+%DF%EA%F3%F8%EA%E8%ED into Олег Якушкин . How to peform string transfer from Олег Якушкин into %CE%EB%E5%E3+%DF%EA%F3%F8%EA%E8%ED using ActionScript? (Its ok if a space character as %20, not + , PHP should handle that fine.)
you can encode it using ActionScript's escape() method.