Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
45
rated 0 times [  52] [ 7]  / answers: 1 / hits: 19959  / 12 Years ago, fri, april 13, 2012, 12:00:00

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:



Ru00e4nisipelgasu00f6u00f6sel


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?


More From » php

 Answers
16

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.


[#86256] Thursday, April 12, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marques

Total Points: 366
Total Questions: 108
Total Answers: 111

Location: Burundi
Member since Wed, Nov 25, 2020
4 Years ago
;