Jump to content

HELP! Session problem passing form variables between pages


lauryn1298

Recommended Posts

Hello,

 

I have a problem passing the values of the form variables between pages when I want to save them in session.

I'm using sessions for saving another values that don't come from the form, and all of them are saved in the session correctly. For example I'm saving the user and the profile once the user is logged, and I pass this value through all the pages without any problem. But now I have to save some values of a form, just in case the user wants to come back and see the values that previously had inserted.

When I print the session in the page where I'm reading the form variables, all of them are set correctly, but when I go back they are empty, but the ones that I had previously are saved correctly.

I was looking for a solution in different forums, I change the name of the variable of the session (at the beginning the name of the session variable and the variable form were the same, and this was giving me problems). I did everything I can think, but the problem is still there.

I'm not sure if it's a problem with the global variables.... In my php.ini are off, and I wouldn't like to change this.

 

Many thanks

Link to comment
Share on other sites

My source code is very big, but I'll try to summarize.

 

page1.php

 

I have previous files where I saved some values on session.

 

<?php

session_start();

include('constants.php');

 

if (!isset($_SESSION['user'])) {

  header("Location: ".RELATIVE_PATH."/field_test/login.php");

  exit;

} else {

    //Calculate the time passed

    $previous_date = $_SESSION["last_access"];

    $actual_date = date("Y-n-j H:i:s");

    $time_pass = (strtotime($actual_date)-strtotime($previous_date));

 

    //Compare the time passes

    if($time_pass >= 28800) {

    //If 20 minutes or more

      session_destroy(); // destroy session

 

      //Send to the login user page

      header("Location: ".RELATIVE_PATH."/field_test/index.php");

 

      //if not, update the date of the session

    }else {

    $_SESSION["last_access"] = $actual_date;

}

}

$profile = $_SESSION['profile'];

?>

........

 

......

 

//Start the html code and the form

<form action="page2.php" method="POST" name="report_gestion" >

<table border="0" width="100%">

<tr>

<td > X :</td>

<td > <select name='x_axis'>

<option label=" " value=""> </option>

<option label="Pair of Sockets" value="pair_sockets">Pair of Sockets</option>

<option label="Max Phy Rate(Mbps)" value="phy_max">Max Phy Rate(Mbps)</option>

<option label="Min Phy Rate(Mbps)" value="phy_min">Min Phy Rate(Mbps)</option>

<option label="Avg Phy Rate(Mbps)" value="phy_avg">Avg Phy Rate(Mbps)</option>

<option label="Avg Throughput(Mbps)" value="xput_avg">Avg Throughput(Mbps)</option>

<option label="Max Throughput(Mbps)" value="xput_max">Max Throughput(Mbps)</option>

<option label="Min Throughput(Mbps)" value="xput_min">Min Throughput(Mbps)</option>

</select>

</td>

 

<td ><input type="checkbox" name="parameters[]" value="hpav" title="HPAV" />HPAV</td>

<td ><input type="checkbox" name="parameters[]" value="mx" title="MX" disabled/>MX</td>

</tr>

<tr>

<td colspan="2"> </td>

</tr>

<tr>

<td > Y :</td>

<td > <select name='y_axis[]' multiple size= 5 >

<option label="Max Phy Rate(Mbps)" value="phy_max">Max Phy Rate(Mbps)</option>

<option label="Min Phy Rate(Mbps)" value="phy_min">Min Phy Rate(Mbps)</option>

<option label="Avg Phy Rate(Mbps)" value="phy_avg">Avg Phy Rate(Mbps)</option>

<option label="Avg TCP Throughput(Mbps)" value="xput_max">Avg TCP Throughput(Mbps)</option>

<option label="Max TCP Throughput(Mbps)" value="xput_max2">Max TCP Throughput(Mbps)</option>

<option label="Min TCP Throughput(Mbps)" value="xput_min">Min TCP Throughput(Mbps)</option>

<option label="Counters" value="sel_primitives">Counters</option>

</select>

</tr>

</td>

</table>

</form>

 

If I do print_r($_SESSION); in this file I obtain:

 

Array ( [profile] => 1 [last_access] => 2010-3-26 12:34:16 [user] => 1 );

 

what is correct.

 

Then I send this form to the page2.php

 

<?php

//Initialize the session for saving the user

session_start();

require_once '../Smarty/libs/Smarty.class.php';

include('../constants.php');

if (!isset($_SESSION['user'])) {

  header("Location: ".RELATIVE_PATH."/field_test/login.php");

  exit;

} else {

    //Calculate the time passed

    $previous_date = $_SESSION["last_access"];

    $actual_date = date("Y-n-j H:i:s");

    $time_pass = (strtotime($actual_date)-strtotime($previous_date));

 

    //Compare the time passes

    if($time_pass >= 28800) {

    //If 20 minutes or more

      session_destroy(); // destroy session

 

      //Send to the login user page

      header("Location: ".RELATIVE_PATH."/field_test/index.php");

  exit;

      //if not, update the date of the session

    }else {

    $_SESSION["last_access"] = $actual_date;

}

}

$profile = $_SESSION['profile'];

 

//Read the parameters from the form

$_SESSION['x_axis_session'] =  $_POST['x_axis'];

$x_axis_param = $_SESSION['x_axis_session'];

 

$_SESSION['y_axis_session'] =  $_POST['y_axis'];

$y_axis_param = $_SESSION['y_axis_session'];

 

$_SESSION['parameters_session'] =  $_POST['parameters'];

$params = $_SESSION['parameters_session'];

 

.... I made different calculation correctly

 

If I do print_r($_SESSION); in this file I obtain:

 

Array (  [profile] => 1 [last_access] => 2010-3-26 12:39:40 [user] => 1 [x_axis_session] => pair_sockets [y_axis_session] => Array ( [0] => phy_max ) [parameters_session] => Array ( [0] => hpav ) )

 

what is correct.

 

But when I come back to page1.php and I print the session I obtain:

Array ( [[profile] => 1 [last_access] => 2010-3-26 12:41:31 [user] => 1[x_axis_session] => [y_axis_session] => [parameters_session] => )

 

Here is where I have the problem, the variables of the form haven't been saved in the session, I don't know why.

 

I hope the code helps. Let me know if you need more details

Link to comment
Share on other sites

Because the array index names exist but the values are empty, it is likely that your code contains a logic error that is setting the values to an empty string.

 

To pin down where in your code the problem is, where (start, end, or other) you are putting the print_r($_SESSION) statement in each of the two files? The problem is either after the print_r() in last file or before the print_r() in the page1 file.

Link to comment
Share on other sites

In page1.php I put print_r($_SESSION) at the beginnig, after session_start(); and in page2.php I put print_r($_SESSION) in the last line of the code. To be sure nothing is happening between the 2 pages. I think is something with the global variables, but I don't know where is the problem.

What do you mean by logic error?

 

I haven't be grateful for your quick replies, thanks a lot, this problem is getting me crazy!

Link to comment
Share on other sites

A logic error that could cause the symptom would be something like -

 

if($_SESSION[....] = '') // sets the session variable to an empty string

 

instead of -

 

if($_SESSION[....] == '') // tests if the session variable is an empty string

 

What does a phpinfo() statement show for the register_globals setting? If register_globals are on, it would also take having other variables (get, post, cookie, or program) with the same name as the session indexes to cause this problem.

 

Frankly, it would take seeing all the relevant code that gets executed on both pages to be able to quickly see what the problem is. This could be due to a page being requested twice, redirect going to a page the redirects again due to a log in problem, a redirect that does not have an exit statement after it and the remainder of the code on the page is clearing the variables, you have a session_write_close statement before the variables are set and even though the print_r() shows them they are not actually part of the session variables, or a number of other things... that someone looking at your code could instantly pick out.

 

Edit: Another possibility (assuming the code you posted for page2, up to the point where those session variables are being set it accurate) would be if page2 gets requested a second time either by your browser or through it being included or redirected to since it is not checking if the form was submitted before setting the session variables from the form data.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.