how can I javascript decodeURI in PHP? - php

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'));

Related

Sending unicode from JQuery ajax function to php results in question marks

Sending Unicode values from an html form (using JQuery ajax to PHP) results in question marks. The value is send using $.ajax and data = form.serialize(). The data looks like this before sending it to PHP %d9%86%D9%83 and so on. I tried to encode the value in the PHP side using many functions, but no luck.
I can provide a sample link if any one can help.
Thanks
Zib Nimer
Is your PHP file saved as Encode UTF-8?
Try using encodeURIComponent(fld.val()) and send it by itself and see what you get:- instead of serialize(), just to test.

How to properly encode UTF-8 for JavaScript and JSON?

I have a problem creating an input validation hash. JavaScript submits data to API and API validates the sent data with json_encode. Basically it works like this:
$input=array('name'='John Doe','city'=>'New York');
$validationHash=sha1(json_encode($input).$key); // Key is known to both servers
If PHP connects to another server, then everything works. It also works through JavaScript (I have a custom sha1() function there as well):
var validationHash=sha1(JSON.stringify({'name':'John Doe','city'=>'New York'})+key);
My problem comes when the string contains UTF-8 characters. For example, if one of the values is:
Ränisipelgasöösel
Then PHP server that receives the command converts it to this after JSON encoding:
R\u00e4nisipelgas\u00f6\u00f6sel
I need to do this in JavaScript as well, but I haven't been able to work out how. I need to make sure that I send proper validation hash to the server or the command fails. I found from Google that unescape(encodeURIComponent(string)) and decodeURIComponent() could be used, but neither gives me the same string that PHP has and validates with.
UTF-8 is used on both client and server.
Any ideas?
It does not seem to be possible. The only working solution I have found is to encode all data with encodeURIComponent() on browser side and with rawurlencode() on PHP side and then calculate the JSON from these values in arrays.
My fix was to raw url encode my json data like so.
rawurlencode( json_encode( $data ) );
And then from within javascript decode the raw url encoded json and then parse the json string like so.
JSON.parse( decodeURIComponent( data ) );
Hope this helps.
Why not base64 encode the data for safe transport? It encodes UTF-8 characters in a safe string that can be sent across different mediums - php, javascript etc. That way you can base64 decode the string at the receiving end. Voila!
By base64 encoding the data, i mean base64 encoding the values and not the whole json string
is you html page encoding utf-8?
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

Posting JSON from hidden form field

I am intercepting a form post using jQuery. With the form fields I am creating a JSON object which is stored in a hidden form field. The value that is passed in to the form field is similar to the following:
{"Status" : "Closed", "Location" : "Glasgow", "Date" : "2012-02-15"}
But if I echo the object from the $_POST variable:
echo $_POST['JSON'];
It output's the following:
{\"Status\" : \"Closed\", \"Location\" : \"Glasgow\", \"Date\" : \"2012-02-15\"}
I have tried running this through stripslashes() and urldecode() but I have had no joy. I understand that I could just replace the back slashes with a replace function but thats a bit too much of a hack.
Has anyone came across this malfored JSON across post before?
Note: This is on the back end of a Wordpress site. I am unsure if that would cause this effect.
Looks like you server has magic_qoutes_gpc 'on'. (http://www.php.net/manual/en/security.magicquotes.what.php)
I came over the same problem once and all I did was using JSON.stringify() to store it as a "String" in my hidden Field and reading the output with jquery.parseJSON() method. Maybe this helps you ! With stringify you can also define a replacer for your JSON Object.
var myJSONText = JSON.stringify(myObject, replacer);
http://www.json.org/js.html
http://api.jquery.com/jQuery.parseJSON/
Although my English is not good, but I see it is the issue of json in php, you can use json_decode do, can be transformed into an array
Another possibility you have is to url-encode with encodeURIComponent() in javascript your json object and urldecode() in php the received object.
Be aware that encodeURIComponent() in js is not exactly the same as urlencode() in php and similarly decodeURIComponent() is not the same as urldecode(), but in most cases encoding in js and decoding in php and vice-versa works well.

Passing get parameter from javascript to php destroys formatting

I format text with javascript asigning + to every emtpy space like this
var ft = text.replace(/ /g,"+");
Then I pass ft to a php script via jquery ajax as an get argument.
But
print $_GET['text'];
gives me the text with empty spaces instead +.
Any ideas?
You should get familiar with the concept of URL encoding.
PHP's urldecode function will run against all $_GET variables by default, so if you want to see raw input, use rawurldecode:
$encoded = array_map('rawurldecode', $_GET);
echo $encoded['text']; //blah+blah
Also, it's a good idea to use JSON to pass data from javascript to PHP.

Cyrillic characters from javascript cookie to php output via $_COOKIE

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

Categories