Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Problems passing a serialized array in a session variable

array session session variable

  • Please log in to reply
3 replies to this topic

#1 gszauer

gszauer

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 112 posts

Posted 14 February 2008 - 02:38 PM

Hi all, i have a multidimentional array i want to pass as a session variable.
Of course i serialized the array.
I have 2 files, set.php & get.php
set.php populates the array, and get.php prints it out. below is my code:

Set.php:
<?php
session_start();
$array = $_SESSION['array'];

if ($array) {
$associative = unserialize($array);
print "Array Exists <br />";
} else {
$associative = array();
print "Array Created <br />";
}

$associative[0] = array();
$associative[1] = array();
$associative[2] = array();
$associative[0][0] = "0-0";
$associative[0][1] = "0-1";
$associative[1][0] = "1-0";
$associative[1][1] = "1-1";
$associative[2][0] = "2-0";
$associative[2][1] = "2-1";


$_SESSION['array'] = serialize($array)
?>


Get.php:
<?php
session_start();
$array = $_SESSION['array'];

if ($array) {
$associative = unserialize($array);
print "Array Exists <br />";
} else {
$associative = array();
print "Array Created <br />";
}

for ($i = 0; $i < count ($associative); $i++){
print "Associative array: ".$associative[$i];
print "Subarray1: ".$associative[$i][0];
print "Subarray2: ".$associative[$i][1];
}

?>


So i navigate to set.php, then to get.php but nothing prints....
What am i doint wrong?
  • 0

It is the mark of an educated mind to entertain a tought without accepting it

If my post was helpful, please help me build some rep Posted Image

#2 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 15 February 2008 - 06:59 AM

Do you have display_errors on? It would seem to me that you have a fatal error somewhere and are not receiving the error message because this is turned off.

Check this tutorial:
http://forum.codecal...ugging-php.html
  • 0

#3 Guest_Jaan_*

Guest_Jaan_*
  • Guest

Posted 15 February 2008 - 08:04 AM

This is good also Jordan but it would be easier to just use


ini_set("display_errors", true);

or

ini_set("display_errors", "1");


and if you want to turn errors off you can delete that line or change true to false and 1 to 0
  • 0

#4 gszauer

gszauer

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 112 posts

Posted 15 February 2008 - 09:18 AM

Nope, not the problem.
I re-vamped the code, now when i refresh the page it should show me a new entery...
<?php
ini_set("display_errors", true);
session_start();
// id, and color_selection are passed via the $_GET method

print "--- Debug info ---<br />";
print "Passed color: ".$_GET['color_selection']."<br />";
if ($_SESSION['color']) {$color = unserialize($_SESSION['color']); print "Color array loaded <br />";} else {$color = array(); print "Color array created <br />";}

for ($i = 0; $i < count ($color); $i++) {
print "<br />--- Reading Array <br /> Index: ".$i."<br />ID: ".$color[$i]["id"]."<br />Value: ".$color [$i]["value"]."<br />";
}

$arraySize = count ($color);
$color[$arraySize] = array();
$color[$arraySize]["id"] = $_GET['id'];
$color[$arraySize]["value"] = $_GET['color_selection'];
print "<br />--- Writing to Array --- <br />Index: ".$arraySize."<br />ID: ".$color[$arraySize]["id"]."<br />Value: ".$color [$arraySize]["value"]."<br />";

$_SESSION['color'] = $color;

?>


Its live at: http://szauer.net/sc..._selection=this
  • 0

It is the mark of an educated mind to entertain a tought without accepting it

If my post was helpful, please help me build some rep Posted Image





Also tagged with one or more of these keywords: array, session, session variable

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download