0

I need to convert a javascript array into a string readable by php "unserialize".

In my javascript, I have:

var params = {};
params['showTitle'] = 0;
params['class'] = '';
params['ajax'] = 0;
params['ajaxDom'] = '';
params['what'] = 'defi';
params['color'] = 'dark';

The output I really need to get is this :

a:6:{s:9:"showTitle";i:0;s:5:"class";s:0:"";s:4:"ajax";i:0;s:7:"ajaxDom";s:0:"";s:4:"what";s:4:"defi";s:5:"color";s:4:"dark";}

Why do I need this? because the output is red on the server side (with php function unserialize) and I cannot change this (it's part of a framework).

My question is : how to convert the javascript array? I've tried (but didn't work):

params = JSON.stringify(params);
params = $.param(params);

It should be easy but I'm stuck... Thank you !

5
  • Can't you put in another PHP script in between? Seems rather complicated to simulate PHP's serialize(), when there is JSON ... Commented Sep 3, 2014 at 19:59
  • 1
    FYI: Letting unserialize loose on parameters coming from the outside is not recommended – it can be a security risk, because it can under certain conditions be used to initiate objects from classes in the script. A framework should not be doing this in the first place. Commented Sep 3, 2014 at 20:01
  • Sirko: I might, I need to try... CBroe: it's Base64 encode as well! I hope it make things safer ?!?! Commented Sep 3, 2014 at 20:07
  • @Siouw Base64 encoding has nothing to do with security! Commented Sep 3, 2014 at 20:10
  • Sirko: oops... getting tired (it's kinda late here...) Commented Sep 3, 2014 at 20:11

3 Answers 3

1

Please see if u can make use of this

http://phpjs.org/functions/serialize/

Sign up to request clarification or add additional context in comments.

1 Comment

I can't get it works :( But I think I might try jstice4all's solution.
1

Just send that js array to the server as JSON, then parse it with json_decode and serialize it with serialize function. Then you can unserialize it.

4 Comments

But I can't change the php part :(
But can you insert a intermediate php part into the the flow? Seems like you can, the javascript is sending it off somewhere, it could send it off to the intermediate php script as suggested before that intermediate php script sends it to the place javascript was sending.
Ok. Then Sunand's answer is more suitable in your case.
ssnobody: Oh right :) I really need some sleep... sorry! jstice4all: I'll try that first thing tomorrow morning! thx
0

This function/answer assumes a lot: mainly that the only two types of data in your JavaScript "array" will be integers and strings, none the less it produces the type of output you demonstrated that the PHP unserialize function requires.

var params = {};
params['showTitle'] = 0;
params['class'] = '';
params['ajax'] = 0;
params['ajaxDom'] = '';
params['what'] = 'defi';
params['color'] = 'dark';

function jsArrToPhpSerializedString( arrObj ) {
  var str_parts = ['', '', '}'],
      arrLen = 0,
      ret = '';

  for (var prop in arrObj) {
    var val = arrObj[prop];
    arrLen += 1;
    str_parts[1] += 's:' + prop.length + ':"' + prop + '";';
    if (typeof val == 'number') {
      str_parts[1] += 'i:' + val + ';';
    } else if (typeof arrObj[prop] == 'string') {
      val = (!val) ? '""' : '"' + val + '"';
      str_parts[1] += 's:' + arrObj[prop].length + ':' + val + ';';
    }
  }

  str_parts[0] = 'a:' + arrLen + ':{';
  ret = str_parts.join('');
  return ret;
}

console.log( jsArrToPhpSerializedString(params) );
// "a:6:{s:9:"showTitle";i:0;s:5:"class";s:0:"";s:4:"ajax";i:0;s:7:"ajaxDom";s:0:"";s:4:"what";s:4:"defi";s:5:"color";s:4:"dark";}"

2 Comments

It's exactly, absolutely, perfectly what I needed... works like a charm ! Thank youuuuuuuuu
No problem. Just beware of the type pitfalls I mentioned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.