SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Miami, Florida - Caracas, Venezuela
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sessions not working.

    What could prevent sessions from working?

    I know it's a broad question but they were working on our old server and they dont now.

    Everything else works fine but this...

    Thank you,
    Luis

  2. #2
    SitePoint Zealot LiamW's Avatar
    Join Date
    Mar 2003
    Location
    Hamilton, Ontario
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Make sure you have session_start() at the top of your script because session_auto_start may be off in your new servers php config.

  3. #3
    SitePoint Zealot Optimum's Avatar
    Join Date
    Apr 2003
    Location
    Miramar
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    session_start();
    with the semi-colon at the top of your page in your php tags.

  4. #4
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Miami, Florida - Caracas, Venezuela
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep...
    PHP Code:
    session_start();
    if(isset(
    $HTTP_POST_VARS['year'])){
     
    $_SESSION['yr'] = $HTTP_POST_VARS['year'];
     
    $_SESSION['mk'] = '';
     
    $_SESSION['md'] = '';
    }
    if(isset(
    $HTTP_POST_VARS['make'])){
     
    $_SESSION['yr'] = $HTTP_POST_VARS['year'];
     
    $_SESSION['mk'] = $HTTP_POST_VARS['make'];
     
    $_SESSION['md'] = '';
    }
    if(isset(
    $HTTP_POST_VARS['model'])){
     
    $_SESSION['yr'] = $HTTP_POST_VARS['year'];
     
    $_SESSION['mk'] = $HTTP_POST_VARS['make'];
     
    $_SESSION['md'] = $HTTP_POST_VARS['model'];

    For a moment there I thought that was it but nope!
    Luis

  5. #5
    SitePoint Zealot LiamW's Avatar
    Join Date
    Mar 2003
    Location
    Hamilton, Ontario
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well what version of php are you running?

    If you have an older version of php you might not be able to use the $_SESSION array as it may not be supported.

    On a side note I don't see the point of using $_SESSION with $HTTP_POST_VARS

    The new arrays ($_SESSION, $_POST, $_GET etc) will only work on newer versions of php so you should either use the new arrays or the old arrays. Mixing them is pointless. If you want to make your scripts compatible with older versions of php use the old arrays.

  6. #6
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Miami, Florida - Caracas, Venezuela
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the side note. I wasn't really paying atention. I fixed it.

    Anyways even though I changed all to work on older versions I still have the same problem.

    I'm using PHP Version 4.1.2.

    Thank you,
    Luis

  7. #7
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ummm....Are you checking those INPUTs before you persist them I wonder ?Doesn't look like it, in which case it might be a good idea to do just now - since I assume your script depends on those right ?Tip : always question INPUT sent by a user, doesn't matter even if it's 'simply a date' etc

  8. #8
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Miami, Florida - Caracas, Venezuela
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The thing is this script was working on my old server so I suspect the problem is in the way I have it configured.
    I don't know if this helps but here's what phpinfo() shows under "Sessions":
    Session Support
    enabled
    session.auto_start
    On
    session.cache_expire
    180
    session.cache_limiter
    nocache
    session.cookie_domain
    no value
    session.cookie_lifetime
    0
    session.cookie_path
    /
    session.cookie_secure
    Off
    session.entropy_file
    no value
    session.entropy_length
    0
    session.gc_maxlifetime
    1440
    session.gc_probability
    1
    session.name
    PHPSESSID
    session.referer_check
    no value
    session.save_handler
    files
    session.save_path
    /tmp
    session.serialize_handler
    php
    session.use_cookies
    On
    Luis

  9. #9
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Miami, Florida - Caracas, Venezuela
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok,

    I inserted:
    PHP Code:
    print_r($GLOBALS); 
    and found Livingston is right. The problem is not the sessions but my POST variables. For some reason they are not being passed.

    I changed the forms action to GET and it worked fine.

    Any ideas?
    Luis

  10. #10
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes

    replace $HTTP_POST_VARS with $_POST

    PHP Code:
    session_start();
    $_SESSION['yr'] = '';
    $_SESSION['mk'] = '';
    $_SESSION['mk'] = '';

    if(isset(
    $_POST['year'])){
    $_SESSION['yr'] = $_POST['year'];
    }
    if(isset(
    $_POST['make'])){
    $_SESSION['mk'] = $_POST['make'];
    }
    if(isset(
    $_POST['model'])){
    $_SESSION['md'] = $_POST['model'];

    - website

  11. #11
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Miami, Florida - Caracas, Venezuela
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Negative...

    That didn't do it. The script is not even getting the values.

    I'll post a more complete example in a couple of minutes.

    Thanks,
    Luis

  12. #12
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Miami, Florida - Caracas, Venezuela
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here it is

    index.php
    PHP Code:
    require 'db.php';
    require 
    'global.php';
    require 
    'functions.php';

    rail_vehicle_selector($_SESSION['yr'],$_SESSION['mk'],$_SESSION['md']); 
    global.php
    PHP Code:
    session_start();
    if(isset(
    $_POST['year'])){
     
    $_SESSION['yr'] = $_POST['year'];
     
    $_SESSION['mk'] = '';
     
    $_SESSION['md'] = '';
    }
    if(isset(
    $_POST['make'])){
     
    $_SESSION['yr'] = $_POST['year'];
     
    $_SESSION['mk'] = $_POST['make'];
     
    $_SESSION['md'] = '';
    }
    if(isset(
    $_POST['model'])){
     
    $_SESSION['yr'] = $_POST['year'];
     
    $_SESSION['mk'] = $_POST['make'];
     
    $_SESSION['md'] = $_POST['model'];

    funtions.php
    PHP Code:
    function rail_vehicle_selector($session_year = '',$session_make = '',$session_model = ''){
     global $db;
     ?>
     <img src="/media/image/rail_title_active_vehicle.gif" width="125" height="16"><div id="rail_vehicle_selector">
     <table width="115" border="0" cellspacing="0" cellpadding="0">
      <tr>
       <td>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="rail_vehicle_selector_year">
         <select name="year" onChange="rail_vehicle_selector_year.submit();">
          <option value="">Year</option>
          <?php
          $year_sql
    =" (SELECT year 
             FROM vehicle,tire_fitment
             WHERE vehicle.vehicle_id=tire_fitment.vehicle_id
             ORDER BY year DESC)
             UNION
             (SELECT year
             FROM vehicle,wheel_fitment
             WHERE vehicle.vehicle_id=wheel_fitment.vehicle_id
             ORDER BY year DESC)"
    ;
          
    $year_rs=mysql_query($year_sql,$db);
          while(
    $year=mysql_fetch_array($year_rs)){
           echo 
    "   <option value=\"" $year['year'] . "\"";
           if(
    $year['year'] == $session_year){
            echo 
    "selected";
           }
           echo 
    ">" $year['year'] . "</option>\n";
          }
          
    ?>
         </select>
        <input name="make" type="hidden" value="">
        <input name="model" type="hidden" value="">
        <noscript>
        <input type="submit" name="Submit" value="Submit">
        </noscript>
        </form>
       </td>
      </tr>
      <tr>
       <td>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="rail_vehicle_selector_make">
          <?php
          
    if(isset($session_year) && $session_year != ''){//if cookies IS set and populated
           
    $make_sql=" (SELECT manufacturer.name AS manufacturer, vehicle.year AS year, vehicle.manufacturer_id
              FROM vehicle,tire_fitment
              LEFT JOIN manufacturer ON manufacturer.manufacturer_id = vehicle.manufacturer_id
              WHERE vehicle.vehicle_id=tire_fitment.vehicle_id AND year = " 
    $session_year "
              ORDER BY manufacturer)
              UNION
              (SELECT manufacturer.name AS manufacturer, vehicle.year AS year, vehicle.manufacturer_id
              FROM vehicle,wheel_fitment
              LEFT JOIN manufacturer ON manufacturer.manufacturer_id = vehicle.manufacturer_id
              WHERE vehicle.vehicle_id=wheel_fitment.vehicle_id AND year = " 
    $session_year "
              ORDER BY manufacturer)"
    ;
           
    $make_rs=mysql_query($make_sql,$db);
           echo 
    "<select name=\"make\" onchange=\"rail_vehicle_selector_make.submit();\">";
           echo 
    " <option value=\"\">Make</option>";
           while(
    $make=mysql_fetch_array($make_rs)){
            echo 
    "   <option value=\"" $make['manufacturer_id'] . "\"";
            if(
    $make['manufacturer_id'] == $session_make){
             echo 
    "selected";
            }
            echo 
    ">" $make['manufacturer'] . "</option>\n";
           }
          }else{
           echo 
    "<select name=\"make\" disabled>";
           echo 
    " <option value=\"\">Make</option>";
          }
          
    ?>
         </select>
         <input name="year" type="hidden" value="<?php echo $session_year?>">
         <input name="model" type="hidden" value="">
        <noscript>
        <input type="submit" name="Submit" value="Submit">
        </noscript>
        </form>
       </td>
      </tr>
      <tr>
       <td>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="rail_vehicle_selector_model">
         <?php
         
    if((isset($session_year) && $session_year != '') && (isset($session_make) && $session_make != '')){//if cookies IS set and populated
          
    $model_sql=" (SELECT manufacturer.name AS manufacturer,model.name AS model, vehicle.model_id
              FROM vehicle,tire_fitment
              LEFT JOIN manufacturer ON manufacturer.manufacturer_id = vehicle.manufacturer_id
              LEFT JOIN model ON model.model_id = vehicle.model_id
              WHERE vehicle.vehicle_id=tire_fitment.vehicle_id AND vehicle.year = " 
    $session_year " AND manufacturer.manufacturer_id = '" $session_make "'
              ORDER BY year)
              UNION
              (SELECT manufacturer.name AS manufacturer,model.name AS model, vehicle.model_id
              FROM vehicle,wheel_fitment
              LEFT JOIN manufacturer ON manufacturer.manufacturer_id = vehicle.manufacturer_id
              LEFT JOIN model ON model.model_id = vehicle.model_id
              WHERE vehicle.vehicle_id=wheel_fitment.vehicle_id AND vehicle.year = " 
    $session_year " AND manufacturer.manufacturer_id = '" $session_make "'
              ORDER BY year)"
    ;
          
    $model_rs=mysql_query($model_sql,$db);
          echo 
    "<select name=\"model\" onchange=\"rail_vehicle_selector_model.submit();\">";
          echo 
    " <option value=\"\">Model</option>";
          while(
    $model=mysql_fetch_array($model_rs)){
           echo 
    "   <option value=\"" $model['model_id'] . "\"";
           if(
    $model['model_id'] == $session_model){
            echo 
    "selected";
           }
           echo 
    ">" $model['model'] . "</option>\n";
          }
         }else{
          echo 
    "<select name=\"model\" disabled>";
          echo 
    " <option value=\"\">Model</option>";
         }
          
    ?>
         </select>
         <input name="year" type="hidden" value="<?php echo $session_year?>">
         <input name="make" type="hidden" value="<?php echo $session_make?>">
        <noscript>
        <input type="submit" name="Submit" value="Submit">
        </noscript>
        </form>
       </td>
      </tr>
     </table>
     </div>
    <?php
    }
    Luis

  13. #13
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    PHP Code:
    session_start();
    if(!isset(
    $_SESSION['yr'])){
    $_SESSION['yr'] = $_POST['year'];
    $_SESSION['mk'] = '';
    $_SESSION['md'] = '';
    }
    .
    .
    # rest of script using my changes 
    I use this method (above) and it works all the time, where as simply using isset($_POST[...]) doesn't always return false is it's empty.

    See how it goes okay ?

  14. #14
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Miami, Florida - Caracas, Venezuela
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are going to kill me!

    I removed:
    Code:
    enctype="multipart/form-data"
    from the forms and it worked.

    No idea why but it fixed it!

    Thank you very much for your help,
    Luis

  15. #15
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    Still... I'd change the method you've used for your sessions though....

  16. #16
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Miami, Florida - Caracas, Venezuela
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, the reason why I check for POST and not the sessin variables is because as soon as a user sellects a year I reload the page setting the selected year and clearing the make and model.

    Then they have to select a manufacturer followed by the model.

    The model list is dependant on the selected manufacturer and the manufacturer is dependant on the year so I have to make sure I clear the "dependant" variables based on what they choose.
    Luis

  17. #17
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh.... I see now


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •