2

So a PHP file returns a string ( to an ajax call ) like this :

$output = $sessID."###".$sessEmail."###".$sessFirstName."###".$sessLanguage."###".$sessRememberMe;

and in javascript i do :

    if (reply.indexOf("###") >= 0) {
    arrayReply = reply.split("###");
    user.ID = arrayReply[0];
    user.Email = arrayReply[1];
    user.FirstName = arrayReply[2];
    user.Language = arrayReply[3];
    user.RememberMe = arrayReply[4];
    }

a problem can arise when parts of reply contain the the delimiter i use "###". What can I do in such a situation? Making the delimiter more complex/rare is not a solution in my opinion.

PS: I did try JSON but it's WAY SLOWER server side.

FINAL EDIT:

server side JSON is slower, and the same for client side, however it's not going to be a bottleneck ( 430ms for 100.000 calls ) and plus there is no need as Jules said below to re-invent the wheel. There was one more solution: bin2hex() in php [which reduced the time from 430ms to 240] and then get back the string in javascript with a hex2string function, however not worth the effort. JSON it is. Thank you all!

MirrorMirror
  • 186
  • 8
  • 36
  • 70
  • 2
    why dont you just send those values from php in a json or xml format – Dhiraj May 04 '12 at 09:05
  • JSON makes much more sense in that situation. However, if you insist on a CSV-type, it's quite common to use additional field delimiters, like '"' and make sure (i.e. replace) that they don't appear in the value fields. – Jules May 04 '12 at 09:08

4 Answers4

2

If as you say encoding as JSON is slower than you could try the following,

$output = '"' . some_kind_of_escape_function($sessID).'","'.some_kind_of_escape_function($sessEmail).'","'.some_kind_of_escape_function($sessFirstName).'","'.some_kind_of_escape_function($sessLanguage).'","'.$sessRememberMe.'"';

and of course replace some_kind_of_escape_function with the appropriate php function (e.g. addslashes or mysql_real_escape_string) it has been a while since I've done PHP development so choose the one that best suits your needs

Then it's a simple case of splitting by the comma and removing the quotes

kzhen
  • 3,020
  • 19
  • 21
1

One option is to use JSON object instead.

For PHP (using json_encode):

$output = json_encode(array(
    "sessid" => $sessID,
    "sessEmail" => $sessEmail,
    "sessFirstName" => $sessFirstName,
    "sessLanguage" => $sessLanguage,
    "sessRememberMe" => $sessRememberMe
));

For JS (using jQuery method):

$.getJSON("/path/to/script.php", function(reply) {
    user.ID = reply.sessid;
    user.Email = reply.sessEmail;
    user.FirstName = reply.sessFirstName;
    user.Language = reply.sessLanguage;
    user.RememberMe = reply.sessRememberMe;
});

Otherwise, you can use any other delimiter that possibly won't be found in the fields (or you can replace it throughout the fields). One of the examples is to use symbol of newline (\n).

VisioN
  • 143,310
  • 32
  • 282
  • 281
  • Thank you for your reply, however JSON is much slow on both server side AND client. in server side it's 6 times slower and that is important i think – MirrorMirror May 04 '12 at 09:10
  • @user1032650 How about using `\n` as a delimiter? – VisioN May 04 '12 at 09:14
  • that would be a good solution, however I'm afraid that whatever delimiter i use, a malicious user by javascript can also insert it. – MirrorMirror May 04 '12 at 09:15
  • @user1032650 Strip it with `str_replace` to spaces :) – VisioN May 04 '12 at 09:16
  • @VisioN \n only shifts the problem since you can have \n as string content as well dont you? Or maybe you want to preserve the \n character for later use. – worenga May 04 '12 at 09:19
  • @mightyuhu In session ID, e-mail, one line first name, language and `bool` remember_me? I doubt. – VisioN May 04 '12 at 09:21
  • in those fields yes i won't have a \n but in other general messages such as text there can be all sorts of characters – MirrorMirror May 04 '12 at 09:22
  • 2
    @user1032650 Hm, then I'd prefer JSON as the safest method. Moreover, I don't think that for powerful server there is a problem of making JSON string. 6 times slower than 1 multiple concatenation will compensate when you start replacing and escaping. In case of JSON the client side gets a valid JavaScript object that should not need to be parsed. So, possibly in web technologies JSON will be even faster than other safe serialization/unserialization methods. – VisioN May 04 '12 at 09:37
1

Why develop your own format if there is already one? use Json:

$output = json_encode(array('sessionID'=>$sessID,'sessionEmail'=>sessEmail,'sessionFirstName'=>$sessFirstName,'sessLanguage'=>$sessLanguage,'sessRememberMe'=>$sessRememberMe));

And for the Javsascript Side see http://www.javascriptkit.com/dhtmltutors/ajaxgetpost4.shtml or if your using JQuery etc. your Framework is much likely to have some kind of inbuild functionality such as http://api.jquery.com/jQuery.getJSON/

However if you want to use your ###-Delimiter i'd suggest you reduce it to just "#", for the sake of simplicity and space. After that introduce what is called an escape charater such as "\" So in a prepass you'll parse your input and replace all occurences of # with #, vice versa in the output. You can then Split your String using a special Regex, which only splits by # and not by "#"

worenga
  • 5,776
  • 2
  • 28
  • 50
0

You can use json.

http://php.net/manual/en/function.json-encode.php

How to JSON decode array elements in JavaScript?

Community
  • 1
  • 1
Nauphal
  • 6,194
  • 4
  • 27
  • 43
  • I did try JSON, however i found that it is WAY SLOWER than by simply concatenating strings and splitting into arrays: server side : 6 times slower, client side ( not that important ) 5-9 times slower – MirrorMirror May 04 '12 at 09:08
  • That sounds hard to believe. Obviously, the whole process will be a tad slower, but 6 times sounds like a lot. Besides, I can't see how this could possibly an issue in the real world. If you have such tight performance requirements, why are you using PHP in the first place? :S – Jules May 04 '12 at 09:19
  • I did benchmarks with 100.000 calls. json was 435ms and simply concatenating was 72ms, that's 6 times server side. – MirrorMirror May 04 '12 at 09:23
  • Exactly why is that an issue? You'll end up in that area anyway, once you start sanitising your strings. Don't try to re-invent the wheel, you're encountering one of the problem-areas for which proper serialisation formats (such as XML or JSON) have been invented. – Jules May 04 '12 at 10:21
  • I think you are right, I would have to re-implement JSON and 435 ms for 100.000 calls is not a problem, I doubt I will have 100.000 concurrent users. P.S. you said PHP is generally slow, what would you suggest as a faster alternative ? – MirrorMirror May 04 '12 at 10:39
  • Well, first of all I doubt that your http server would handle 100,000 concurrent AJAX requests very well in the first place. I would not dare to suggest any alternative, as I have no idea what the project is about or what the general requirements are. What I meant was, that if a few CPU cycles are a concern, you probably would look at some highly optimised CGI solution and not interpreted PHP code. Anyway, I guess PHP is fine performance-wise for 98% of the web projects it is used for. – Jules May 04 '12 at 10:59