simonj2 Posted December 16, 2007 Share Posted December 16, 2007 Hi, I'm working on a php site that tracks phone calls. I'm trying to keep that info from page to page. The phone calls are stored as arrays of objects. Here's some code: class call { var $date; var $time; var $origin; var $destination; var $duration; var $type; } ... $calls[$row] = new call(); $calls[$row]->date = $date; $calls[$row]->time = $time; $calls[$row]->origin = $origin; $calls[$row]->destination = $destination; $calls[$row]->duration = $duration; $calls[$row]->type = $type; $row++; .... $callsserialized = serialize($calls); <form method="post" action="preview.php"> [Lines below are in html, don't worry about that] <? echo "<input type='hidden' name='calls' value='$callsserialized'>"; ?> <input type="submit" name="Submit" value="Submit"> Then on the next page (which also has the same class definition as above, in reality it's actually the same page): $calls2 = $_POST["calls"]; $calls = unserialize($calls2); Then if I try for example: $temp = $calls[1]->date; I get nothing. Does anyone have any idea what's going wrong? I've spent a few hours on it, but I can't figure it out? The official PHP manual pages aren't helping me out much either... Merry Christmas to everyone, I'd appreciate any help Simon Quote Link to comment Share on other sites More sharing options...
corbin Posted December 16, 2007 Share Posted December 16, 2007 Try this and see if it works (untested, so no idea if it will or not), and if it does, it should be moddable to your needs, unless you need/want it to be sent through $_POST for some reason or other. page 1 class TheClass { public $var; function __construct($val) { $this->var = $val; } } session_start(); $c = new TheClass('lol'); $_SESSION['class'] = serialize($c); page 2 session_start(); if(isset($_SESSION['class'])) { $c = unserialize($_SESSION['class']); echo $c->var; } The problem with yours is most likely the serialized text being 'corrupted' in the POST transit.... Try echo'ing the serialized data on both pages and see if they look they same. Quote Link to comment Share on other sites More sharing options...
simonj2 Posted December 16, 2007 Author Share Posted December 16, 2007 corbin, that worked! Thank you! It never occured to me to try session cookies! Strange that post would corrupt it though, and kind of worrying. Just for anyone else who wants to use htat, remember to use the session_start(); or it won't work, that messed me up for 10 minutes Quote Link to comment Share on other sites More sharing options...
corbin Posted December 16, 2007 Share Posted December 16, 2007 The problem with passing it through POST with a hidden field is that the serialized string most likely gets messed up... For example, maybe quotes are in it or something, and then the HTML conflicts. if you did htmlentities() on the serialized string, it might work through POST. For example, this works: <?php class TheClass { public $var = 'lol'; } if(isset($_POST['serial'])) { $s = $_POST['serial']; $s = unserialize($s); echo $s->var; } else { $s = new TheClass; $serialized = htmlentities(serialize($s)); echo <<<HERE <form action="" method="POST"> <input type="hidden" name="serial" value="{$serialized}" /> <input type="submit" value="Submit" /> </form> HERE; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.