Ahmed Ebaid
Ahmed Ebaid

Reputation: 417

Sharing a session variable between multiple php scripts

My question is that when I copy my array elements between different PHP scripts using session variables, nothing gets printed out. The following are my two PHP files.

file1.php

<?PHP
     session_start();
        $SQL = "SELECT * FROM tblquestions";

        if ($db_found) {
            $result = mysql_query($SQL);
            $numRows = mysql_num_rows($result); //return number of rows in the table

            echo '<FORM NAME ="form1" METHOD ="POST" ACTION ="file2.php">';
            for ($i = 1; $i <= 2; $i++)
            {
                $db_field = mysql_fetch_assoc($result);
                $qID[$i] = $db_field['QID'];
                $question[$i] = $db_field['Question'];
                $A[$i] = $db_field['qA'];
                $B[$i] = $db_field['qB'];
                $C[$i] = $db_field['qC'];
                echo '<P>';
                print $question[$i];
                echo '<P>';
                echo "<INPUT TYPE = 'Radio' Name = '".$qNum."'  value= 'A'>"; 
                print $A[$i];
                echo '<P>';
                echo  "<INPUT TYPE = 'Radio' Name = '".$qNum."'   value= 'B'>"; 
                print $B[$i];
                echo '<P>';
                echo  "<INPUT TYPE = 'Radio' Name = '".$qNum."'   value= 'C'>"; 
                print $C[$i];
                //if (isset($_POST[$name_Value]))
                $survey_Answers[$i-1] = $_POST[$qNum];
                print '</BR>'.$survey_Answers[$i-1]."</BR>";
                $question_Number = ltrim($qNum,'q');
                $question_Number++;
                $qNum ='q'.$question_Number;
            }

            echo '<p>';
            $_SESSION['answers'] = $survey_Answers;
            echo '<INPUT TYPE = "Submit" Name = "Submit1"  VALUE = "Click here to vote">';

            echo '</form>';
?>

On my Second file (file2.php), I have the following:



<?PHP
    session_start();
    if (isset($_POST['Submit1'])) {
            $results = $_SESSION['answers'];
            print $results[0];
}
?>

However, on my file2.php I get the following error: Undefined offset: 0 and nothing gets printed out.

Upvotes: 1

Views: 2880

Answers (5)

Joy Reynolds
Joy Reynolds

Reputation: 11

There is a comment on the PHP manual page for session_start() which sounds very similar to your problem. http://php.net/manual/en/function.session-start.php#65944

It says that an array with integer keys assigned either as a straight array or using the integers will fail and the data will not pass to the next page.

Modify your code to use strings as keys.

Upvotes: 0

Manoj Yadav
Manoj Yadav

Reputation: 6612

in file1.php session_start(); should be first line of the code same as in file2.php

Upvotes: 0

Jijo John
Jijo John

Reputation: 1375

Please Add the below code to every page you want to use the session data.Other wise it will return an error .

<?php
session_start();
?>

Upvotes: 0

user729928
user729928

Reputation: 713

To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

source: http://php.net/manual/en/function.session-start.php

You need to call session_start() before you output anything. It is a best practice to place it at the beginning of your script before you output anything.

Upvotes: 1

luk2302
luk2302

Reputation: 57214

echo '<p>';
session_start();

that can´t work, session_start has to be called before any output! if you put session_start() at the beginning of your file, you should be all right.

Upvotes: 1

Related Questions