Problem with session variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Garry Jones

    Problem with session variables

    This is a excerpt from the code where I collect data with a form.

    <FORM action="index15 dq.php" method="post" name="stegscf_p wd_edit"
    id="stegscf_pwd _edit">

    I am setting three session variables to already declared variables. I then
    echo them and can see on the resulting form that they are set correctly.

    <?
    session_start() ;
    $_SESSION['scf_upd_pwd'] = $scfmpwd1;
    $_SESSION['scf_upd_num'] = $scfchknum;
    $_SESSION['scf_upd_nam'] = $scfmfor;
    echo $_SESSION['scf_upd_pwd'];
    echo $_SESSION['scf_upd_num'];
    echo $_SESSION['scf_upd_nam'];
    ?>

    On the processing page - index15dq.php - I have this code.

    <?
    error_reporting (E_ALL);
    session_start() ;
    $scfmpwd1 = $_SESSION['scf_upd_pwd'];
    $scfchknum = $_SESSION['scf_upd_num'];
    $scfmfor = $_SESSION['scf_upd_nam'];
    print "values";
    echo $scfmpwd1;
    echo $scfchknum;
    echo $scfmfor;
    ?>

    Its echoing blank values. No errors reported.

    Shouldn't this work? How can I force session varables to work. Does it
    matter where session_start is declared?

    My pages are complex. First I open an index which looks at certain key
    values, the page is then dynamicaly put together using segments of php code,
    certain segments are omited or included according to which page is being
    loaded. Much of these form the surrounding information, the headers, the
    pictures and banners and the meny bar on the left of the screen.). All of
    which can vary accordning to which page is being loaded.

    Eventually the nitty-gritty of the page is loaded in the centre.

    It is here that I am a) declaring loading the session variables.
    and b) trying to use the loaded session variables.

    Somewhere along the way they are being unloaded.

    Garry Jones
    Sweden


  • Rik

    #2
    Re: Problem with session variables

    Garry Jones wrote:
    This is a excerpt from the code where I collect data with a form.
    >
    <FORM action="index15 dq.php" method="post" name="stegscf_p wd_edit"
    id="stegscf_pwd _edit">
    >
    I am setting three session variables to already declared variables. I
    then echo them and can see on the resulting form that they are set
    correctly.
    >
    <?
    session_start() ;
    $_SESSION['scf_upd_pwd'] = $scfmpwd1;
    $_SESSION['scf_upd_num'] = $scfchknum;
    $_SESSION['scf_upd_nam'] = $scfmfor;
    echo $_SESSION['scf_upd_pwd'];
    echo $_SESSION['scf_upd_num'];
    echo $_SESSION['scf_upd_nam'];
    >>
    >
    On the processing page - index15dq.php - I have this code.
    >
    <?
    error_reporting (E_ALL);
    session_start() ;
    $scfmpwd1 = $_SESSION['scf_upd_pwd'];
    $scfchknum = $_SESSION['scf_upd_num'];
    $scfmfor = $_SESSION['scf_upd_nam'];
    print "values";
    echo $scfmpwd1;
    echo $scfchknum;
    echo $scfmfor;
    >>
    >
    Its echoing blank values. No errors reported.
    >
    Shouldn't this work? How can I force session varables to work. Does it
    matter where session_start is declared?

    Yes. print_r($_SESSI ON); to check what your session actually holds.
    Be warned that session_start() has to be declared BEFORE any output, or it
    won't work. So not in 'the centre' of your page, but as one of the first
    things 'on top'. Even sending out a simple space or line-break before
    session_start() will render it useless.

    Another word of advice: don't rely on shorttags: always use <?php instead
    of <?
    --
    Grtz,

    Rik Wasmus


    Comment

    • Garry Jones

      #3
      Re: Problem with session variables

      "Rik" <luiheidsgoeroe @hotmail.comskr ev i meddelandet
      news:a0bbd$4536 3b89$8259c69c$1 1868@news2.tude lft.nl...
      Yes. print_r($_SESSI ON); to check what your session actually holds.
      Be warned that session_start() has to be declared BEFORE any output, or it
      won't work. So not in 'the centre' of your page, but as one of the first
      things 'on top'. Even sending out a simple space or line-break before
      session_start() will render it useless.
      Done that and I am getting this. (Since I posted I turned error reporting on
      in a setting my web hotel service allows users to change).

      Warning: session_start() [function.sessio n-start]: Cannot send session
      cookie - headers already sent by (output started at
      /customers/scfmotion.se/scfmotion.se/httpd.www/sitedefs.php:67 ) in
      /customers/scfmotion.se/scfmotion.se/httpd.www/scfma_edt_pwd.p hp on line 34

      Warning: session_start() [function.sessio n-start]: Cannot send session cache
      limiter - headers already sent (output started at
      /customers/scfmotion.se/scfmotion.se/httpd.www/sitedefs.php:67 ) in
      /customers/scfmotion.se/scfmotion.se/httpd.www/scfma_edt_pwd.p hp on line 34

      So what does
      "session cookie" and "session cache limiter" - mean?

      Okay, it does seem like I have to move session_start somewhere else in the
      code. Thats a bit of a nightmare because the code itself should only have
      session_start based on a couple of conditions. These conditions are met when
      that php code segment is called for. I am going to have to make a copy of
      the very same if statements higher up and start the session earlier. That
      should do it if I have understood you correctly.

      Garry Jones


      Comment

      • Rik

        #4
        Re: Problem with session variables

        Garry Jones wrote:
        Okay, it does seem like I have to move session_start somewhere else
        in the code. Thats a bit of a nightmare because the code itself
        should only have session_start based on a couple of conditions. These
        conditions are met when that php code segment is called for. I am
        going to have to make a copy of the very same if statements higher up
        and start the session earlier. That should do it if I have understood
        you correctly.
        Indeed.
        Things like headers, actions to take, etc, I usually do before any HTML
        output at all.
        --
        Grtz,

        Rik Wasmus


        Comment

        • Garry Jones

          #5
          Re: Problem with session variables

          Rik" <luiheidsgoeroe @hotmail.comskr ev i meddelandet
          news:769e4$4536 474f$8259c69c$1 4655@news2.tude lft.nl...
          Garry Jones wrote:
          Indeed.
          Things like headers, actions to take, etc, I usually do before any HTML
          output at all.
          Hmmm I got one of my forms working. Did a similar thing to another one but
          am still having problems

          On the form page these two variables are echoed correctly.
          <?= $_SESSION['scf_upd_num'] ?>
          <?= $_SESSION['scf_upd_nam'] ?>

          On the form page and the processing page I load the session start write at
          the begining, first line of code.

          But on the processing page these rows

          error_reporting (E_ALL);
          print_r($_SESSI ON);
          echo $_SESSION['scf_upd_num'];
          echo $_SESSION['scf_upd_nam'];

          produce this output

          Notice: Undefined variable: _SESSION in
          /customers/scfmotion.se/scfmotion.se/httpd.www/cent15dt.php on line 5
          Notice: Undefined variable: _SESSION in
          /customers/scfmotion.se/scfmotion.se/httpd.www/cent15dt.php on line 6
          Notice: Undefined variable: _SESSION in
          /customers/scfmotion.se/scfmotion.se/httpd.www/cent15dt.php on line 7

          Note that the form is processed by - FORM action="index15 dt.php" - a file
          which uses a segment called cent15dt.php which can be seen in the above
          example. I am surprised to see the name of the segment in the code because
          the index15dt.php is the start of the code that puts the entire page
          together.

          Any ideas?

          Garry Jones
          Sweden


          Comment

          • Rik

            #6
            Re: Problem with session variables

            Garry Jones wrote:
            Notice: Undefined variable: _SESSION in
            /customers/scfmotion.se/scfmotion.se/httpd.www/cent15dt.php on line 5

            This is an error you'll get when you fail to give a correct
            session_start() . This is mandatory on EVERY page. Be sure to:

            - include session_start() in your script as one of the first command in
            every file offcourse.
            - enable error_reporting before the session_start() ;
            - beware of type-errors :-)
            --
            Rik Wasmus


            Comment

            Working...