Jump to content

[SOLVED] serialize


simonj2

Recommended Posts

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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;
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.