Search code examples
phpsessionmultidimensional-arraysession-variablesoutput-buffering

Multidimensional $_SESSION variables not being set before header redirect


I have a basic form submission script that sets multidimensional $_SESSION variables (2 levels) from a loop - then redirects using header location.

I developed this on my local machine (xampp/windows server running PHP 5.3.8) without problems but I am having issues with it on my web linux server running PHP 5.3.3 and even upgraded to 5.3.21 with same issues.

here is my script:

if($_POST['submitted']){


    $_SESSION['c'] = array();
    $_SESSION["RETURNING_DISCOUNT"] = array();
    $seq = 1;

    for($d=1; $d<=10; $d++){

        if($_POST["COMPANY_$d"] && $_POST["PHONE_$d"]){

            $_SESSION['c'][$seq] = array();

            $_SESSION['c'][$seq]["COMPANY"] = str_replace("\'", "", str_replace('\"', '', $_POST["COMPANY_$d"]));
            $_SESSION['c'][$seq]["PHONE"] = str_replace("\'", "", str_replace('\"', '', $_POST["PHONE_$d"]));

            if($_POST["COUNTRY_$d"] == 'Canada'){
                $_SESSION['c'][$seq]["STATE"] = str_replace("\'", "", str_replace('\"', '', $_POST["STATE_$d"]));
            }else{
                $_SESSION['c'][$seq]["STATE"] = str_replace("\'", "", str_replace('\"', '', $_POST["STATE_2_$d"]));
            }
            $_SESSION['c'][$seq]["COUNTRY"] = $_POST["COUNTRY_$d"];
            $_SESSION['c'][$seq]["ADDY1"] = str_replace("\'", "", str_replace('\"', '', $_POST["ADDY1_$d"]));
            $_SESSION['c'][$seq]["ADDY2"] = str_replace("\'", "", str_replace('\"', '', $_POST["ADDY2_$d"]));
            $_SESSION['c'][$seq]["CITY"] = str_replace("\'", "", str_replace('\"', '', $_POST["CITY_$d"]));
            $_SESSION['c'][$seq]["ZIP"] = str_replace("\'", "", str_replace('\"', '', $_POST["ZIP_$d"]));
            $_SESSION["RETURNING_DISCOUNT"][$_SESSION['c'][$seq]["COMPANY"]] = $_POST["RETURNING_COMPANY_$d"];

            $seq++;

        }//end declare var in $_SESSION

    }//end for


                header("location: register3.php");



}//end usersubmit

And what it sets is following:

[RETURNING_DISCOUNT] => Array
    (
        [working] => 1
    )

[c] => 11

What it should set (and does set if I disable header location) is:

[RETURNING_DISCOUNT] => Array
        (
            [working] => 1
        )

    [c] => Array
        (
            [1] => Array
                (
                    [COMPANY] => jk
                    [PHONE] => jhgk
                    [STATE] => 
                    [COUNTRY] => 
                    [ADDY1] => 
                    [ADDY2] => 
                    [CITY] => 
                    [ZIP] => 
                )

        )

So as you can see the issue is with the $_SESSION['c'] array. Like I said it works when you disable header("location: register3.php");. Its almost as if the page redirects before the multidimensional array values are properly set.

Could this be a output_buffering issue? I have output_buffering = off in my php.ini file. I also turned it on to see if it would make a difference but no luck.

Any insight to this would be much appreciated. Thanks.


Solution

  • Passerby set me on the right path. register3.php was the culprit. I still don't understand how the same code rendered differently on two servers.

    here is what I had:

    for($c=0; $c<=10; $c++){
        if($_SESSION['c'][$c]){
            $all_co[] = "taco";
        }//end if something is found in array
    }//end for
    

    and when I changed the var $c to $x everything worked fine. I'm glad I finally solved this but still unsettled as to why this happened.