I have way too many hours into this and I feel as though it is due to a configuration difference somewhere between the two servers. Here is code for two simple scripts:

TEST1.PHP

<?php
session_start();

$cart[0] = array('item_number' => '12345', 'qty' => '5');

$_SESSION['cart'] = serialize($cart);

header('Location: test2.php');
exit();
?>

TEST2.PHP

<?php
session_start();

print_r($_SESSION);
print_r($_COOKIE);

$cart = unserialize($_SESSION['cart']);

print_r($cart);

phpinfo();
?>

The scripts behave differently on different machines...

If you go to http://tim.copelanddata.net/test1.php , you will see that the session gets set correctly, and you can refresh test2.php and get the same results.

However, when you go to http://tim.belczak.net/test1.php, the session gets set and is displayed correctly the first time. If you refresh test2.php, the session disappears. It does NOT disappear if unserialize() is not called.

I have included the session and cookie variables as well as phpinfo() for debugging. Please let me know if you can spot the obvious, which I cannot.

Thanks!

Recommended Answers

My understanding is that sessions handle serialization for you. Are you sure that you need it? Maybe you are interfering with the Session internal serialization process.

Jump to Post

All 3 Replies

Based on what I found from php.net, you can try this.

<?php
session_start();

$cart[0] = array('item_number' => '12345', 'qty' => '5');

$_SESSION['cart'] = base64_encode(serialize($cart));

header('Location: test2.php');
exit();
?>
<?php
session_start();

print_r($_SESSION);
print_r($_COOKIE);

$cart = unserialize(base64_decode($_SESSION['cart']));

print_r($cart);

phpinfo();
?>

My understanding is that sessions handle serialization for you. Are you sure that you need it? Maybe you are interfering with the Session internal serialization process.

My understanding is that sessions handle serialization for you. Are you sure that you need it? Maybe you are interfering with the Session internal serialization process.

I tried it without the serialization and it worked just fine! I first started using serialized arrays in sessions in 2005 with PHP4 and have always used serialize and unserialize. In know this host just migrated to new servers, so I wonder if an environment variable was tweaked to self-serialize arrays in sessions. You saved me from another evening of frustration! Thank you, thank you, thank you!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.