Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php: session disappears after page navigation

Tags:

php

session

I've read all the posts I could find about this issue, but, to date, none of the solutions have worked for me. Obviously, I'm overlooking something important. I also don't know how to debug sessions. I read one article, PHP session Debugging, but it was over my head.

So, much like the other issues, when I navigate to another page in my app, whether through a link or a form submit, my session disappears. I have no idea why my session vanishes. If someone has the time to help me investigate, it would be greatly appreciated.

These are my php.ini settings

; Name of the session (used as cookie name).  
session.name = PHPSESSID  

; The path for which the cookie is valid.  
session.cookie_path = /

This is the first view to display

<?php
session_start();
if (!isset($_SESSION['session_id'])) {
    $_SESSION['session_id'] = session_id();
}
if (!isset($_SESSION['invoices'])) {
    $_SESSION['invoices'] = $invoices;
}

if (isset($_SESSION['session_id'])) {
    print_r($_SESSION['session_id'] . " in invoiceList.<br />");
} else {
    echo 'No session ID set in invoiceList <br />';
}
?>
<div>

    <table>
        <tr>            
            <th>Customer Name</th>
            <th>Invoice Date</th>
            <th>Invoice Number</th>
        </tr>
        <tr>
            <?php
            include_once 'form/editInvoice.php';
            if (isset($invoices)) {

                foreach ($invoices as $invoice) {
                    ?>
                <tr>
                    <td><?php echo $invoice['customer_name'] ?></td>
                    <td><?php echo $invoice['invoice_date'] ?></td>
                    <td><?php echo $invoice['invoice_number'] ?></td>
                    <td><a href='<?php echo $_SERVER['SCRIPT_NAME']; ?>/retrieve?class=InvoiceLineItems&amp;id=<?php echo $invoice['invoice_id']; ?>'><?php echo $invoice['invoice_id']; ?></a></td>
                </tr>
                <?php
            }
        } else {
            echo 'No invoices retrieved.';
        }
        ?>
        </tr>
    </table>
</div>  

Here is the included form:

<?php
session_start();

if (isset($_SESSION['session_id'])) {
    print_r($_SESSION['session_id'] . "in editForm<br />");
} else {
    echo 'No session ID set in editForm <br />';
}

if (!$_POST) {
    ?>

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
        <fieldset>
            <legend>Enter Updated PO Number</legend>
            <li>PO Number: <input type="text" name="po_number"/></li>
        </fieldset>
        <input type="submit" value="Submit" />
        <input type="button" onclick="alert('Changes Canceled.')" value="Cancel"/>
    </form>
<?php }
?>

And finally, the detail page for when the user clicks a link in the main page.

<?php
session_start();

if (isset($_SESSION['session_id'])) {
    print_r($_SESSION['session_id'] . "<br />");
} else {
    echo 'No session ID set invoice<br />';
}
?>

<h1>Invoice Detail</h1>
<div>
<?php
foreach ($partnerInfo as $info) {

    switch ($info['role_indicator']) {
        case 'remit_to':
            ?>
            <div id="remit">
                <ul>
                    <li>PLEASE REMIT TO:</li>
                    <li><?php echo $info['partner_name']; ?></li>
                    <li><?php echo $info['street_name']; ?></li>
                    <li><?php echo $info['city_name']; ?>, <?php echo $info['state']; ?> <?php echo $info['postal_code']; ?></li>

                </ul>
            </div>
            <?php break; ?>
        <?php case 'seller': ?>
            <div id = "seller" >
                <ul>
                    <li>Service Site:</li>
                    <li><?php echo $info['partner_name']; ?></li>
                    <?php
                    if ($info['partner_aux_info'] !== NULL) {
                        ?><li><?php echo $info['partner_aux_info']; ?>
                        <?php }
                        ?>
                    </li>
                    <li><?php echo $info['street_name']; ?></li>
                    <li><?php echo $info['city_name']; ?>, <?php echo $info['state']; ?> <?php echo $info['postal_code']; ?></li>
                    <li>(405)677-0221</li>
                </ul>        
            </div>
            <?php break; ?>
        <?php case 'sold_to': ?>
            <div id="buyer">
                <ul>
                    <li>Bill To: </li>
                    <li><?php echo $info['partner_name']; ?></li>
                    <li><?php echo $info['street_name']; ?></li>
                    <?php
                    if ($info['suite_info'] !== NULL) {
                        ?><li><?php echo $info['suite_info']; ?>
                        <?php }
                        ?>
                    </li>
                    <li><?php echo $info['city_name']; ?>, <?php echo $info['state']; ?> <?php echo $info['postal_code']; ?></li>
                </ul> 
            </div>   
            <?php break; ?>
        <?php
    }
}
?>
<h1>Line Items</h1>
<table>
    <th>PO Number</th>
    <th>PO Issued Date</th>
    <th>Description</th>
    <th>Service Start Date</th>
    <th>Service End Date</th>
    <th>Shipped Date</th>

    <?php foreach ($invoiceLineItems as $lineItem) { ?>
        <tr>
            <td><?php echo $lineItem['po_number']; ?></td>
            <td><?php echo $lineItem['po_issued_date']; ?></td>
            <td><?php echo $lineItem['line_item_name']; ?></td>
            <td><?php echo $lineItem['service_period_start']; ?></td>
            <td><?php echo $lineItem['service_period_end']; ?></td>
            <td><?php echo $lineItem['request_for_delivery']; ?></td>
        </tr>       
        <?php
    }
    ?>

</table>


</div>

Edit: I've removed the session checks and updated the code sample. I've added session_start() before my <head> tag in index.php. I've verified that I can write to the session temp folder.

When i execute this code in my controller to update the invoices with the new PO number, I reach the model's function, but the session is gone.

//If form is posted, update line items with PO number and date.
if (isset($_POST['po_number'])) {
    $this->invoice->update();
}

By the time I reach the session variable assignment, I have no session data:

public function update() {

$con = $this->_getLocalConn();


$invoices = $_SESSION['invoices'];

try {
    $sqlUpdate = $con->prepare("UPDATE invoices 
                                SET po_number = ?, po_issued_date = ?
                                WHERE invoice_id = ?");

    foreach ($invoices as $record) {
        $sqlUpdate->execute(array(
            $_POST['po_number'],
            getdate(),
            $record['invoice_id']
        ));
    }
} catch (PDOException $e) {
    print $e->getMessage();
}
//get the PO number being used to update the records
//perform db update where po_number = input
//notify user of success and display updated records.

}

like image 550
Mike S. Avatar asked Jan 26 '26 10:01

Mike S.


2 Answers

Each PHP file should start with session_start(); regardless of $_SESSION being set or not. This function will create a new session OR take up the existing one.

like image 100
Paul Avatar answered Jan 28 '26 22:01

Paul


No $_SESSION is started to begin with when you check it with your first if. Therefore, it will always FAIL. You must call session_start() PRIOR to doing anything with a $_SESSION variable. Correct your code.

First page:

<?php
session_start();

/* Don't need this unless you really need the debugging
   Previously you where assigning variables that did not 
   exist to the the $_SESSION variables. Not what you want
   I imagine.

if (!isset($_SESSION)) {
    var_dump($_SESSION);

}
*/
...

Include form:

<?php
session_start();

if (isset($_SESSION['session_id'])) {
    print_r($_SESSION['session_id'] . "in editForm<br />");
} else {
    echo 'No session ID set in editForm <br />';
}
...

Detail page:

<?php
session_start(); //Notice a pattern here??


if (isset($_SESSION['session_id'])) {
    print_r($_SESSION['session_id'] . "<br />");
} else {
    echo 'No session ID set invoice<br />';
}
?>
like image 44
PenguinCoder Avatar answered Jan 28 '26 22:01

PenguinCoder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!