faqts : Computers : Programming : Languages : PHP : Common Problems : Forms and User Input

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

21 of 24 people (88%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

How can I post an object to a PHP script? (example: ...action="form.php?$object)

Aug 26th, 2000 02:10
Guillermo Roditi, Adolfo Manuel Pachón Rodríguez, Ben Udall, http://php.net/manual/function.serialize.php http://php.net/manual/function.unserialize.php


use serialize() and unserialize() look for them in the php man. It also 
works for arrays, but you shouldnt be needing to transfer objects 
anyway, its really a design error. i also understand that it doesnt 
allways work, look at this comment from the php manual:
/**************************/
I've also been having some difficulty in using this function with an 
object. 
I have a form that posts variable to a page which declares an instance 
of a class, sets the class instance's internal variables from global 
form variable, then attempts to serialize the object, urlencode it, 
pass it in the url to another page, and I get the error "The page 
cannot be displayed" I'm beginning to think it may be the size of the 
object I am passing. When I do this same thing with a simple integer, 
there are no errors. 
The code I'm using is: 
$object = new class; 
$object->read(); // stuffs object with vars from $GLOBALS[] 
$serialized_object = serialize($object); 
$urlencoded_object = urlencode($serialized_object); 
Header("Location:/nextpage.phtml?object_pass=$urlencoded_object"); 
On the next page I have 
$serialized_object = urldecode($object_pass); 
$object = new class; 
$object = unserialize($serialized_object); 
Like I said, if I use the same procedure without classes, everything 
goes fine...
/**************************/
and this one:
/**************************/
class Unserializable{
  function unserialize($str){ 
    $ary=unserialize($str); 
    for(reset($ary);$kv=each($ary);){ 
      $key=$kv[key]; 
      if (gettype($this->$key)!="user function") 
      $this->$key=$kv[value]; 
    } 
  } 
} 
/**************************/