Javascript->PHP serializer





6
Date Submitted Mon. Jun. 4th, 2007 3:06 PM
Revision 1 of 1
Scripter Fordiman
Tags "object passing" | JavaScript | PHP | serialize
Comments 6 comments
This is a quick set of overrides for Javascript so that any variable can be passed to PHP in a GET/POST activity. Just call myVar.toPHP();

I use this little set of functions extensively in a little Javascript/PHP RPC handler I wrote. I don't have the reverse function, as I pass JSON back to the browser for the return value.

Note: This lib is not safe for binaries or HTML Elements. The former will come out similar to FTP ASCII breaks, and the latter will cause infinite recursion. If you want to make a speical case for HTML Elements, do so; you could probably just test for parentNode and create a 'safe' object from that. I didn't need it, so I didn't code it.

As for binary safety, at some point between toPHP/escape/post/urldecode/unserialize, the object breaks. Rather than create a huge fix for something I didn't need to do, I put in a quick match/hack. Don't like it? Write the fix yourself.

Object.prototype.toPHP = function () {
        var retf='O:8:"stdClass":';
        var reta=':{';
        var ct=0;
        var last;
        for (var i in this) {
                if (typeof this[i] == 'function') continue;
                reta+=i.toPHP()+this[i].toPHP();
                ct++;
        }
        return retf+ct+reta+'}';
}
String.prototype.toPHP = function () {
/*
Make sure all strings have network-style carraige
returns; if we don't, PHP doesn't recognize the
length as correct. As a result, the String type
is not binary safe. What are you doing passing
binaries via POST anyway?? Use the normal file
uploading for cripes sake.
*/

        var s=this.replace(/((?:\r|\n|\r\n))/g,'\r\n');
        var i=-1,ct=0;
        while ((i=s.indexOf('\n',i+1))!=-1) ct++;
        return 's:'+(s.length-ct)+':"'+s+'";';
}
Boolean.prototype.toPHP = function () {
        return 'b:'+(this==true?1:0)+';';
}
Number.prototype.toPHP = function () {
        /* Only make it a double if we actually need to */
        return (this==Math.floor(this)?'i':'d')+':'+this.toString()+';';
}
Date.prototype.toPHP = function () {
        /* Changes into an int compatible with mktime() */
        return 'i:'+Math.floor(this.getTime()/1000)+';';
}
Array.prototype.toPHP = function () {
        var ret='a:'+this.length+':{';
        for (var i=0; i<this.length; i++)
                ret+=i.toPHP()+this[i].toPHP();
        return ret+'}';
}
Function.prototype.toPHP = function () {
        return null;
}
 

Comments

Comments Example
Thu. Sep. 27th, 2007 7:24 AM    Scripter Fordiman
Comments More details please
Thu. Jun. 14th, 2007 1:48 AM    Beginner stutimandal
  Comments Subject My Comment
Mon. Aug. 27th, 2007 1:44 AM    Newbie subrata
    Comments fgfd
Mon. Aug. 27th, 2007 2:48 AM    Newbie subrata
Comments Hi
Wed. Aug. 1st, 2007 1:50 AM    Beginner arabindindaa
Comments nice
Thu. Jun. 14th, 2007 12:02 AM    Syntax Master sundaramkumar

Voting