La variable de sesión de PHP se elimina a la mitad del script

I'm trying to save a session variable in one of my script and it seems to work fine for a portion of the script, however, it gets dropped in the second part of my code. I've echoed every line and can't figure out why this is happening. Also, this code was working just fine before, but ever since I switched servers, it seems to be malfunctioning. I don't think it's the session save path because the first part of the code works fine. Here is the basic outer structure of my code (I've removed all the irrelevant parts to save space):

<!--raw-->
<?php 

session_start(); 
include 'config.php'; 

?>
<html>
<body>

<form id="driver_record" method="post" action="<?php echo htmlentities($PHP_SELF); ?>">
Enter Driver Number: <input type="text" name="driver" />
<input type="submit" value="Submit" name="user_submit" />
</form>

<?php

if(isset($_POST['user_submit']))
{
if(username_exists($_POST['driver'])){

$ulog = $_POST['driver'];
$_SESSION['user_id'] = $ulog;
echo $_SESSION['user_id']; //Session echos fine here
$tablename_cc = "cc_".$ulog;
$tablename_db = "db_".$ulog;
$tablename_misc = "misc_".$ulog;
$tablename_cash = "cash_".$ulog;

?>

<form id="expenses_update" method="post" action="<?php echo htmlentities($PHP_SELF); ?>">
<input type="submit" value="Approve" name="record_approve" />

<?php
}
}
?>

<?php

    if(isset($_POST['record_approve']))
    {
        echo $_SESSION['user_id']; //Session does not echo here
        $ulog = $_SESSION['user_id'];
        $tablename_cc = "cc_".$ulog;
        $tablename_db = "db_".$ulog;
        $tablename_misc = "misc_".$ulog;
        $tablename_cash = "cash_".$ulog;

    }

    //unset($_SESSION['user_id']);
    //session_destroy();
?>

</body>
</html>
<!--/raw-->

EDIT: I have commented out the unset statement, and also tried putting them inside the if statement, with no change to the behavior of the code.

preguntado el 24 de agosto de 12 a las 07:08

can you drop php version and ini config please, because you'r code work well on my server, also check what you do in config.php -

I dropped the config from the code and things still behave the same. What do you mean by drop the PHP version? -

4 Respuestas

Everytime the code is run you are calling the unset($_SESSION['user_id']); this is because it is not within your IF statement, you have a curly brace in the wrong place.

<?php

    if(isset($_POST['record_approve']))
    {
        echo $_SESSION['user_id']; //Session does not echo here
        $ulog = $_SESSION['user_id'];
        $tablename_cc = "cc_".$ulog;
        $tablename_db = "db_".$ulog;
        $tablename_misc = "misc_".$ulog;
        $tablename_cash = "cash_".$ulog;

    unset($_SESSION['user_id']);
    session_destroy();

    } //moved curly brace here

?>

EDIT: The code runs fine for me, I had to comment out the include line and the function used, the fault must be within the config.php. Do you have a session_start() at the top of that file and no unsets used ?

Respondido 24 ago 12, 08:08

Sorry, I should have mentioned that the unset has been placed inside the if statement, and it has also been commented out, but the code still behaves exactly the same. - user1562781

No, I don't have a session on top of that file. Is it necessary? - user1562781

I just removed the config file and the behavior is the same. Unless it's something to do with my host that I'm not aware of, I really don't know. Could anything server side affect session behaviors? - user1562781

are you getting the same results when running this local ? - AlanW

I don't have PHP set up locally. This is all running off of a virtual server. - user1562781

Here is a simple session script, are you having problems with this ? If so then there is a problem with php.

<?php
session_start();

$_SESSION['testing'] = "boo";
echo "Session 1=" . $_SESSION['testing'];
?>

<form action="session1.php" method="post">
<input type="text" name="input"
<input type="submit" />
</form>
<pre>

<?php

if (isset($_POST['input']))
{
   print_r ($_SESSION);
   print_r ($_POST);
}

?>

Respondido 25 ago 12, 11:08

It seems that neither I or anyone else here can find a logical solution as to what's happening. I've decided to simply remove the session variables and include a 'hidden' input field that just sends the variable through a POST variable. This seems to solve all my problems without having to worry about the behavior of the session variables.

Respondido 25 ago 12, 23:08

You set the session value only when the first form is submitted (isset($_POST['user_submit'])). When the script is processing the second form submission (isset($_POST['record_approve'])), no session value is set. Hence no output.

You do realize that, even if the script is in one PHP file, it will be triggered by two mutually exclusive form submissions, right? So the session value will be available only after isset($_POST['user_submit']. So if you submit the second form after submitting the first one, you will have a session variable 'user_id', provided you have commented out the lines that unset and destroy the session.

Espero que tenga sentido.

Respondido 24 ago 12, 07:08

Isn't the purpose of session variables to be kept until destroyed? One thing I should mention is that the last two lines where I unset the variable and destroy the session have been commented out, and the behavior is still the same. - user1562781

Also, this exact structure was working before, so it makes no sense to me. - user1562781

The way the script is handled, the second form can only be triggered after the first, so 'user_id' should be available because session variables shouldn't disappear until destroyed (at least from what I understand). - user1562781

Pero tu unset y session_destroy are outside the form processing loops, so will be triggered every time the script is called. - futurogeek

I've commented those out already. The behavior is exactly the same. I've also had them inside the loop, and still the same. - user1562781

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.