0

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.

2 Answers 2

2

Remove your session_start() and at the beginning of your functions.php put this:

if (!session_id()) {
    session_start();
}

For use from a plugin use this:

function register_session(){
    if( !session_id() )
        session_start();
}
add_action('init','register_session');
3
  • I was reading lots on adding to functions.php. But since this is a stand alone plugin to be used on multiple websites, I need to create a action that adds it to my functions.php.I find lots of plugin that will add things to my function.php, but nothing to add to my theme / function.php via plugin.
    – davidb3rn
    Sep 1, 2020 at 18:50
  • Ok, I updated my aswer
    – drcrow
    Sep 2, 2020 at 0:12
  • Yes, this worked! I needed it add it into my plugin function file, not more individual shortcodes. Thank you for the solution. I kept thinking of "function.php" as a theme file only. Thank you
    – davidb3rn
    Sep 2, 2020 at 14:39
0

There's absolutely no need to do all of this of the answer above, simply use session_start() at the very top of your script, that will either retrieve the existing session, or start a new one if no one exists, all by itself (check here). You simply need to call it at the very top of the script wherever you use $_SESSION content. So, use session_start() at the top of the script where you insert whatever into your $_SESSION superglobal, and then also call session_start() at the very top of the script where you wanna use it. works always, and gets particularly easy when you use page templates and custom themes. Also, adding session initiations into your functions.php file will open sessions on ANY of your pages, not just the ones where you want that, so you may do more than you would actually need. You can also simply call session_start() in the corresponding plugin pages / scripts, etc.

4
  • I have tried ` add_action('wp_head', 'start_my_session'); function start_my_session() { if (session_status() == PHP_SESSION_NONE) { echo "Session did not exist, starting session..."; session_start(); } } ` I used an action to make sure it was at the top of the page, however it says a session already started, based on my echo. But my session I created still doesn't carry through between pages. When I do it just at the top of my plugin page (which is a short code) I get this Warning: session_start(): Cannot start session when headers already sent
    – davidb3rn
    Sep 2, 2020 at 14:34
  • alright, question then, are you using page templates for your pages?
    – DevelJoe
    Sep 2, 2020 at 14:36
  • I am not. The solution was where I create my session function. I was doing it in the shortcode file itself, when I needed tosimply add it in a global place like my plugin start file (or my plugin function.php) I don't know proper name for it. Thank you for your response.
    – davidb3rn
    Sep 2, 2020 at 14:41
  • no worries, just trynna help. However, if I may say, it's a good practice to work with page templates for such reasons; facing and handling such problems then becomes terribly easy by just saying session_start() at the top of each page where you wanna use session data. Solutions which do so in the main plugin file or the main theme file (functions.php) as you decided to do work, but are not really the best solution, because these files are triggered on all pages, if via plugin maybe even frontend and backend (except coded differently in plugin / theme file); and you most likely don't want that
    – DevelJoe
    Sep 2, 2020 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.