0

I am currently trying to pass a multidimensional array through sessions while also being able to dynamically add/remove from it (it is a wish-list). The index of the array will be the ID of an item I am adding to the array. I've tried serialization, using variables instead of the actual session and none of it worked properly for me.

My issue is that my array will not pass from page 1 to page 2. Page 1 is what happens when the user clicks any "add to wish-list" button

I searched up on Google and wrote something similar to this:

page 1:

session_start();
$_SESSION['wishlist'] = array();
$id = $_GET['id'];
$imageFileName = $_GET['ImageFileName'];
$title = urldecode($_GET['PictureName']);

$_SESSION['wishlist'][$id]=array("id"=>$id,"title"=>$title,"imageFileName"=>$imageFileName); // Here im making the multidimensional array

$_SESSION['wishlist'] = $_POST; //found this way on Stackoverflow

header('Location:view-wish-list.php'); //move to second page

page 2: trying to start session and print out the array to test:

session_start();


var_dump($_SESSION['wishlist']);

Var Dump gives me array(0) { }

Any help would be appreciated. Thank you.

1
  • 1
    Remove $_SESSION['wishlist'] = $_POST;. You are making a GET request and $_POST would obviously be empty. Commented Mar 15, 2020 at 19:04

1 Answer 1

1

You have to commit (write) the session before redirection or the second request may occur before the session data is available on the server:

session_write_close();
header('Location:view-wish-list.php'); //move to second page
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.