Jump to content

I have a question about session variables


jim.davidson

Recommended Posts

I'm having a problem, my code works fine when I run it on my localhost.  When I load it to the "live" server some session variables disappear.  I did a check a find that my localhost is running version 5.2.0 but the "live" server is running version 4.4.7.  Could this be my problem? Do the two version handle session variables differently?

 

Any guidance will be appreciated!

Page one has a dropdown menu to select a customer by name

 

if (!isset($_SESSION)) {

  session_start();

}

$cust_id = '';

 

This is the part of the code where $cust_id comes from

 

      <td><div align="left">

        <select name="cust_id" id="cust_id" class="textbackground">

          <option value="" <?php if (isset($error) && isset($_POST['cust_id']) == $cust_id) { echo 'selected="selected"';}

          elseif ($firstid == $cust_id) {echo "selected=\"selected\"";} ?>>          </option>

          <?php

          do { ?>

          <option value="<?php echo $row_getCust['customer_id']?>"<?php if (!(strcmp($row_getCust['customer_id'], $firstid)))

              {echo "selected=\"selected\"";} ?>>

            <?php echo $row_getCust['last_name']?> <?php echo $row_getCust['first_name']?> <?php echo $row_getCust['mi']?>

            </option>

          <?php

            } while ($row_getCust = mysql_fetch_assoc($getCust));

            $rows = mysql_num_rows($getCust);

            if($rows > 0) {

              mysql_data_seek($getCust, 0);

          $row_getGetCust = mysql_fetch_assoc($getCust);

          }?>

        </select>

      </div></td>

 

After customer is selected from the dropdown menu, I store $cust_id to session variable

 

if (!empty($_POST['cust_id'])) {

      $_SESSION['cust_id'] = $cust_id;

}

 

Now I go to another page to display that customer's info

 

if (!isset($_SESSION)) {

  session_start();

}

echo 'customer id =  ';

echo $_SESSION['cust_id'];

 

When I'm on the localhost the result is - customer id = 4

 

When I'm on the "live" server the result is - customer id =

 

The only thing I can say right now after looking at that is that you don't need to do this:

if (!isset($_SESSION)) {
  session_start();
}

 

session_start() creates a session or resumes the current one. Therefore, there's no need for any if statements. You say session_start() at the top of every page that you're using session variables on. At the very top. Before anything else. If there is a session, it resumes it. If not, it starts a new one.

 

The manual

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.