I am trying to take my existing object array then json_encode it and add it to a $_SESSION[''] so I can request it on other pages on my website. Is there a way to save my session string (or array) and display it on another page?
Below is the code of the array I am trying to add into a session. ( I don't know if I need to encode it, but I thought it would increase performance perhaps?)
$postArray = array(
"CompanyID" => "5",
"FirstName" => $_POST['first_name'],
"LastName" => $_POST['last_name'],
"Email" => $_POST['email'],
"Company" => $_POST['company'],
"Phone" => $_POST['phone'],
"Fax" => $_POST['fax'],
"AdressLine1" => $_POST['address'],
"AdressLine2" => "",
"City" => $_POST['city'],
"DistrictID" => $_POST['state'],
"CountryID" => $_POST['country'],
"PostCode" => $_POST['postcode'],
"SpecialInstructions" => $_POST['special'],
"Items" =>
$item_array
,
"Source" => "Web submission"
);
$json = json_encode($postArray);
Then right after that code I try to initialize an action to wp to start and add to session
add_action('wp', 'start_my_session');
function start_my_session() {
session_start();
$_SESSION['order_details'] = $GLOBALS['json'];
}
After that I try to call it on a new page with this code
<?php
add_action('wp_footer', 'show_session_var');
function show_session_var() {
if(isset($_SESSION['order_details'])) echo $_SESSION['order_details'];
}
?>
When I try to retrieve the data I get this error: session_start(): Cannot start session when headers already sent but I need to create a session because wordpress doesn't automatically make one?
Any thoughts? I am new to php development in Wordpress.