session not working on a php page

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

    session not working on a php page

    Hi All,

    I am trying to experiment with sessions and they seem to be resetting
    everytime i visit the page.

    here is the code of the page

    <?php
    session_start() ;
    $count = $_SESSION["counter"] + 1;
    $_SESSION["counter"] = $count;
    echo $_SESSION["counter"];
    ?>

    if I refresh the page, the count doesn't increase. Whast should i do
    to fix this?

    any help is much appreciated

    cheers
    Suman
  • Michael Fesser

    #2
    Re: session not working on a php page

    ..oO(Suman)
    >I am trying to experiment with sessions and they seem to be resetting
    >everytime i visit the page.
    >
    >here is the code of the page
    >
    ><?php
    >session_start( );
    >$count = $_SESSION["counter"] + 1;
    >$_SESSION["counter"] = $count;
    The above two lines could also be written as

    $_SESSION['counter'] = $_SESSION['counter'] + 1;

    or

    $_SESSION['counter'] += 1;

    or simply

    $_SESSION['counter']++;
    >echo $_SESSION["counter"];
    >?>
    >
    >if I refresh the page, the count doesn't increase. Whast should i do
    >to fix this?
    Do you use session cookies? Does your browser accept it?

    Micha

    Comment

    • Suman

      #3
      Re: session not working on a php page

      Thanks for the quick answer Micha.

      I tried simply using

      <?php
      session_start() ;
      $_SESSION['counter']++;
      echo $_SESSION['counter'];
      ?>

      and it only displays 1 no matter how many times i refresh so still not
      working. The browser is set to accept cookies as well.

      suman

      Comment

      • Jerry Stuckle

        #4
        Re: session not working on a php page

        Suman wrote:
        Thanks for the quick answer Micha.
        >
        I tried simply using
        >
        <?php
        session_start() ;
        $_SESSION['counter']++;
        echo $_SESSION['counter'];
        ?>
        >
        and it only displays 1 no matter how many times i refresh so still not
        working. The browser is set to accept cookies as well.
        >
        suman
        >
        If you enable all errors and display them, you'll find you get a notice
        on the first run because $_SESSION['counter'] is not defined.

        This should be:

        <?php
        session_start() ;
        if (isset($_SESSIO N['counter']))
        $_SESSION['counter']++;
        else
        $_SESSION['counter'] = 1;
        echo $_SESSION['counter'];
        ?>

        But other than the original notice, it should work OK.

        You need to enable all errors and display them to find out what your
        real problem is. In your php.ini file, you should have:

        error_reporting =E_ALL
        display_errors= on

        These should be on for all development systems (but display_errors
        should be off on production systems).

        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        • C. (http://symcbean.blogspot.com/)

          #5
          Re: session not working on a php page

          On 13 Apr, 13:15, Suman <YoS...@gmail.c omwrote:
          Hi All,
          >
          I am trying to experiment with sessions and they seem to be resetting
          everytime i visit the page.
          >
          here is the code of the page
          >
          <?php
          session_start() ;
          $count = $_SESSION["counter"] + 1;
          $_SESSION["counter"] = $count;
          echo $_SESSION["counter"];
          ?>
          >
          if I refresh the page, the count doesn't increase. Whast should i do
          to fix this?
          >
          any help is much appreciated
          Find the problem - the code is fine. But the most common cause of
          cookie related problems is output before a header or cookie realted
          statement - PHP is very good at intercepting and reporting this - why
          have you not checked your logs? Do you know how errors are recorded/
          reported?

          Even if that turns up nothing - you should look at the HTTP data
          passing to and from the browser - you can use firebug or tamperdata
          for Firefox, or iehttpheaders for MSIE. Or just sniff the traffic with
          wireshark. Then you can verify if the server is presenting a cookie,
          and if the browsers retruns it in subsequent requests.

          C.

          Comment

          • Suman

            #6
            Re: session not working on a php page

            I have tried the new code


            <?php
            session_start() ;
            if (isset($_SESSIO N['counter']))
            $_SESSION['counter']++;
            else
            $_SESSION['counter'] = 1;
            echo $_SESSION['counter'];
            ?>

            enabled the error trapping and there is no errors.

            There is no output before the session_start, the code is as is on the
            php page. I have had a loook through firebug and the cookie is there
            too.

            Can someone use this code on a webpage and refresh the page to see if
            it works. If it does can you please send me a link. Would be great
            help, this is killing me :)

            Kind regards
            Suman

            Comment

            • Jerry Stuckle

              #7
              Re: session not working on a php page

              Suman wrote:
              I have tried the new code
              >
              >
              <?php
              session_start() ;
              if (isset($_SESSIO N['counter']))
              $_SESSION['counter']++;
              else
              $_SESSION['counter'] = 1;
              echo $_SESSION['counter'];
              ?>
              >
              enabled the error trapping and there is no errors.
              >
              There is no output before the session_start, the code is as is on the
              php page. I have had a loook through firebug and the cookie is there
              too.
              >
              Can someone use this code on a webpage and refresh the page to see if
              it works. If it does can you please send me a link. Would be great
              help, this is killing me :)
              >
              Kind regards
              Suman
              >
              Suman,

              I've done it - as have others who confirmed it works.

              Can't send you a link - it's on my development system, which has
              restricted access.

              When you say you "enabled error trapping" - did you add the lines I
              recommended to your php.ini file?

              --
              =============== ===
              Remove the "x" from my email address
              Jerry Stuckle
              JDS Computer Training Corp.
              jstucklex@attgl obal.net
              =============== ===

              Comment

              • =?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?=

                #8
                Re: session not working on a php page

                Suman escribió:
                <?php
                session_start() ;
                $count = $_SESSION["counter"] + 1;
                $_SESSION["counter"] = $count;
                echo $_SESSION["counter"];
                ?>
                >
                if I refresh the page, the count doesn't increase. Whast should i do
                to fix this?
                From what I've read in the thread it looks like the temporary directory
                for sessions does not exist or is not writeable by the web server. Check
                the "session.save_p ath" directive in your php.ini file.



                --
                -- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
                -- Mi sitio sobre programación web: http://bits.demogracia.com
                -- Mi web de humor al baño María: http://www.demogracia.com
                --

                Comment

                • C. (http://symcbean.blogspot.com/)

                  #9
                  Re: session not working on a php page

                  On 14 Apr, 03:09, Suman <YoS...@gmail.c omwrote:
                  I have tried the new code
                  >
                  <?php
                  session_start() ;
                  if (isset($_SESSIO N['counter']))
                  $_SESSION['counter']++;
                  else
                  $_SESSION['counter'] = 1;
                  echo $_SESSION['counter'];
                  ?>
                  >
                  enabled the error trapping and there is no errors.
                  How do you know? have you deliberately inserted a fatal run-time error
                  (like call_non_exista nt_fn()) and confirmed it gets logged where you
                  expected it?
                  >
                  There is no output before the session_start, the code is as is on the
                  php page.
                  How do you know? A BOM would not show in your text editor but causes
                  output from the script.
                  I have had a loook through firebug and the cookie is there
                  too.
                  This suggests that there is no output before the script. But is the
                  cookie there in both request and reply?

                  Have you checked if the session file is created on the webservers
                  filesystem? Is it getting updated each time?

                  Have you checked that you're not inadvertently caching content?

                  (try <?php print rand(100); ?)

                  C.

                  Comment

                  • google@shopdownlite.com

                    #10
                    Re: session not working on a php page

                    On Apr 14, 7:22 am, "C. (http://symcbean.blogsp ot.com/)"
                    <colin.mckin... @gmail.comwrote :
                    On 14 Apr, 03:09, Suman <YoS...@gmail.c omwrote:
                    >
                    I have tried the new code
                    >
                    <?php
                    session_start() ;
                    if (isset($_SESSIO N['counter']))
                       $_SESSION['counter']++;
                    else
                       $_SESSION['counter'] = 1;
                    echo $_SESSION['counter'];
                    ?>
                    >
                    enabled the error trapping and there is no errors.
                    >
                    How do you know? have you deliberately inserted a fatal run-time error
                    (like call_non_exista nt_fn()) and confirmed it gets logged where you
                    expected it?
                    >
                    >
                    >
                    There is no output before the session_start, the code is as is on the
                    php page.
                    >
                    How do you know? A BOM would not show in your text editor but causes
                    output from the script.
                    >
                    I have had a loook through firebug and the cookie is there
                    too.
                    >
                    This suggests that there is no output before the script. But is the
                    cookie there in both request and reply?
                    >
                    Have you checked if the session file is created on the webservers
                    filesystem? Is it getting updated each time?
                    >
                    Have you checked that you're not inadvertently caching content?
                    >
                    (try <?php print rand(100); ?)
                    >
                    C.

                    Some web servers also require you to declare the session_save_pa th.
                    So check that to make sure it is actually saving the sessions.

                    BDP
                    Down Pillow
                    ShopDownLite.co m

                    Comment

                    Working...