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">
Related
As far as i call recall. A valid Json data comes in this format '{"key":"value"}'
But while surfing, I found an article about sending UTF-8 codes as emoji.
Emoji was stored in variable as
$emoji = "\ud83d\udc4e";. For it to work properly, the answer was to use json_decode($emoji);. I tried it out and it returned a thumbs down emoji. Meanwhile, I was expecting NULL but it turns out that it was a valid json data. So I'm confused how that is possible.
I'm having some troubles with my $_POST/$_REQUEST datas, they appear to be utf8_encoded still.
I am sending conventional ajax post requests, in these conditions:
oXhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
js file saved under utf8-nobom format
meta-tags in html <header> tag setup
php files saved under utf-8-nobom format as well
encodeURIComponent is used but I tried without and it gives the same result
Ok, so everything is fine: the database is also in utf8, and receives it this way, pages show well.
But when I'm receiving the character "º" for example (through $_REQUEST or $_POST), its binary represention is 11000010 10111010, while "º" hardcoded in php (utf8...) binary representation is 10111010 only.
wtf? I just don't know whether it is a good thing or not... for instance if I use "#º#" as a delimiter of the explode php function, it won't get detected and this is actually the problem which lead me here.
Any help will be as usual greatly appreciated, thank you so much for your time.
Best rgds.
EDIT1: checking against mb_check_encoding
if (mb_check_encoding($_REQUEST[$i], 'UTF-8')) {
raise("$_REQUEST is encoded properly in utf8 at index " . $i);
} else {
raise(false);
}
The encoding got confirmed, I had the message raised up properly.
Single byte utf-8 characters do not have bit 7(the eight bit) set so 10111010 is not utf-8, your file is probably encoded in ISO-8859-1.
We have php server that sends json string in utf-8 encoding.
Im responsible for the iphone app that get the data.
I want to be sure that on my side everything is correct :
//after I downlad the data stream :
NSString* content = [[NSString alloc] initWithData:self.m_dataToParse encoding:NSUTF8StringEncoding];
//here the data is shown correctly in the console
NSLog(#"%#",content);
SBJsonParser *_parser = [[SBJsonParser alloc]init];
NSDictionary *jsonContentDictionary = [_parser objectWithData:self.m_dataToParse];
//here, when i printer values of array IN array, i see \u454 u\545 \4545 format. any ideas why ?
for(id key in jsonContentDictionary)
{
NSLog(#"key:%#, value:%#,key, [ jsonContentDictionary objectForKey:key]);
}
im using the latest version of json library :
https://github.com/stig/json-framework/
There is problem is the iphone side ? (json parser ? ) or in the php server ?
just to be clear again :
1.on the console, before json, the string looks o.k
2.after doing json, the array in array values are in the format of \u545 \u453 \u545
Thanks in advance.
Your code is correct.
A possible reason of the issue, and you must investigate it with your content provider (the server that sends the json to you), is that even if the whole json string is correctly encoded as utf-8 (remember: the json text is a sequence of character and so an encoding must be specified), it may happen that some or all of the text content (that is the values of the single objects contained in the json message) has been originally encoded in another format, typically this is html-encoding (iso-8859) especially when particular characters are used (e.g. cyrillic or asian). Now the json framework by default decodes all data as utf-8, but if there is a coding mismatch between the utf-8 characters and the iso-8859 (just to remain in the example) then the only way to transform them in utf-8 is to use the \u format. This happens quite often, especially when php scripts extract the info from html pages, which are usually encoded using iso-8859. And consider also that iOS is not able to convert the whole set of iso-8859 characters to unicode (e.g.: cyrillic).
So possible solutions are:
- do a content encoding of texts server side (iso-8859 --> utf-8)
- or if this is not possible, then it's up to you to recognize the \uxxx sequences coming more often from your content provider and replace them with the corresponding utf-8 characters.
How can I encrypt the data receieved from an ajax request? For example I noticed from Google when a get request is made the data that is receieved is all scambled like :
position:absolute;top:0;width:155px\\x22 onclick\\x3d\\x22google.psy\\x26\\x26google.psy.qs(event)\\x22\\x3e\\x3cdiv id\\x3dms\\x3e\\x3cul\\x3e\\x3cli class\\x3d\\x22mitem msel\\x22\\x3e\\x3cspan class\\x3dmicon style\\x3d\\x22background-position:-20px -85px\\x22\\x3e\\x3c/span\\x3eEverything\\x3cli class\\x3d\\x22mitem\\x22\\x3e\\x3ca href\\x3d\\x22/search?q\\x3dasdfadf\\x26amp;hl\\x3den\\x26amp;prmd\\x3divns\\x26amp;source\\x3dlnms\\x26amp;tbm\\x3disch\\x26amp;ei\\x3dHZy4TeKYG6bw0gG8pbTRDw\\x26amp;sa\\x3dX\\x26amp;oi\\x3dmode_link\\x26amp;ct\\x3dmode\\x26amp;cd\\x3d2\\x26amp;sqi\\x3d2\\x26amp;ved\\x3d0CAgQ_AUoAQ\\x22 class\\x3d\\x22q qs\\x22\\x3e\\x3cspan class\\x3dmicon style\\x3d\\x22background-position:-40px -85px\\x22\\x3e\\x3c/span\\x3eImages\\x3c/a\\x3e\\x3cli class\\x3d\\x22mitem\\x22\\x3e\\x3ca href\\x3d\\x22/search?q\\x3dasdfadf\\x26amp;hl\\x3den\\x26amp;prmd\\x3divns\\x26amp;source\\x3dlnms\\x26amp;tbm\\x3dvid\\x26amp;ei\\x3dHZy4TeKYG6bw0gG8pbTRDw\\x26amp;sa\\x3dX\\x26amp;oi\\x3dmode_link\\x26amp;ct\\x3dmode\\x26amp;cd\\x3d3\\x26amp;sqi\\x3d2\\x26amp;ved\\x3d0CAkQ_AUoAg\\x22 class\\x3d\\x22q qs\\x22\\x3e\\x3cspan class\\x3dmicon style\\x3d\\x22background-position:-80px -85px\\x22\\x3e\\x3c/span\\x3eVideos\\x3c/a\\x3e\\x3cli class\\x3d\\x22mitem\\x22\\x3e\\x3ca href\\x3d\\x22/search?q\\x3dasdfadf\\x26amp;hl\\x3den\\x26amp;prmd\\x3divns\\x26amp;source\\x3dlnms\\x26amp;tbm\\x3dnws\\x26amp;ei\\x3dHZy4TeKYG6bw0gG8pbTRDw\\x26amp;sa\\x3dX\\x26amp;oi\\x3dmode_link\\x26amp;ct\\x3dmode\\x26amp;cd\\x3d4\\x26amp;sqi\\x3d2\\x26amp;ved\\x3d0CAoQ_AUoAw\\x22 class\\x3d\\x22q qs\\x22\\x3e\\x3cspan class\\x3dmicon style\\x3d\\x22background-position:-120px -85px\\x22\\x3e\\x3c/span\\x3eNews\\x3c/a\\x3e\\x3cli class\\x3d\\x22mitem\\x22\\x3e\\x3ca href\\x3d\\x22/search?q\\x3dasdfadf\\x26amp;hl\\x3den\\x26amp;prmd\\x3divns\\x26amp;source\\x3dlnms\\x26amp;tbm\\x3dshop\\x26amp;ei\\x3dHZy4TeKYG6bw0gG8pbTRDw\\x26amp;sa\\x3dX\\x26amp;oi\\x3dmode_link\\x26amp;ct\\x3dmode\\x26amp;cd\\x3d5\\x26amp;sqi\\x3d2\\x26amp;ved\\x3d0CAsQ_AUoBA\\x22 class\\x3d\\x22q qs\\x22\\x3e\\x3cspan class\\x3dmicon style\\x3d\\x22background-position:-120px -105px\\x22\\x3e\\
How can I achieve a similar structure?
you can't.
in the code you posted it's not encryption but just encoding.
It is not really "encrypted" simply obfuscated and encoded. I am unsure of the encoding type being used. Check this SO post, which has a similar question and it states ASCII that is encoded hexadecimal.
The data example isn't scrambled, it's just encoded. Any 'strange' character is replace by \\x## where ## is some hexadecimal number representing that character.
\\x22 is probably a "
\\x3d is probably a =
and so on ..
All,
I make a JSON request to a web server using PHP and it returns me a JSON response in a variable. The JSON response will have lots of keys and values. The JSON response I get from the server has special characters in it. So, I use the following statement to convert it to UTF8,decode the JSON and use it as an array to display to the UI.
$response = json_decode(utf8_encode($jsonresponse));
Now, I have to pass the same value to the server in a JSON request to do some stuff. However, when I pass
$jsonrequest = json_encode(utf8_encode($request));
to the server, it fails.
The following code succeeds in reading special characters and displaying it to the UI. But fails if I have to pass the utf8_encode value to the server.
The current whole roundtrip code is as under:
$requestdata = json_encode($request);
$jsonresponse = //Do something to get from server;
$response = json_decode(utf8_encode($jsonresponse));
How can I modify it such that I pass the exact value as to what I receieved from the json response from the server?
The JSON response I get from the server has special characters in it. So, I use the following statement to convert it to UTF8,decode the JSON and use it as an array to display to the UI.
JSON data already comes encoded in UTF-8. You shouldn't convert it to UTF-8 again; you'll corrupt the data.
Instead of this:
$response = json_decode(utf8_encode($jsonresponse));
You should have this:
$response = json_decode($jsonresponse); //already UTF-8!
Set This into your php connection :
$sql = “SET NAMES ‘utf8′”;
mysql_query($sql, $link);
have you tryed switching the places of the functions?
$jsonrequest = utf8_encode(json_encode($request));
utf8_encode only encodes strings not arrays
You convert the initial request to UTF-8, so I assume it's something else. But when you send the data back, you do not convert it back to the original encoding.
Are you sure you send the data in the encoding expected by the server?
I also use both ZF and utf-8 encoded strings in AJAX calls and I think that the uft8_encode and utf8_decode functions should be obsolete.
Do you have a valid meta tag
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" / >
and a valid doctype
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
?