Sessions

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

    #1

    Sessions

    Dear all,

    I made the following form.

    <FORM METHOD="POST" ACTION="prova.p hp">
    <b>Combinatio n</b><br><br>
    Element 1: <INPUT NAME="el1" TYPE="TEXT">
    <BR>
    Element 2: <INPUT NAME="el2" TYPE="TEXT">
    <br>
    <INPUT TYPE="SUBMIT" VALUE"Submit">
    </FORM>
    <br>
    Combination<br> <br>

    <?php
    session_start() ;
    $_SESSION['combinations'][] = ($_REQUEST["el1"].$_REQUEST["el2"]);
    print_r($_SESSI ON['combinations']);
    ?>

    If I fill the two text fields, reload the page and then fill again the
    two text fields, the new text overwrites the old one.
    I'd like that the array can include both the old and the new text.
    I tried to use the sessions, but it doesn't work.
    Can you help me in solving this problem please?
    Many thanks.

    Cheers,
    Nico
  • theCancerus

    #2
    Re: Sessions

    simple do this
    <?php
    session_start() ;
    $_SESSION['combinations'][] = ($_REQUEST["el1"].$_REQUEST["el2"]);
    print_r($_SESSI ON['combinations']);
    ?>
    <FORM METHOD="POST" ACTION="aktry.p hp">
    <b>Combinatio n</b><br><br>
    Element 1: <INPUT NAME="el1" TYPE="TEXT">
    <BR>
    Element 2: <INPUT NAME="el2" TYPE="TEXT">
    <br>
    <INPUT TYPE="SUBMIT" VALUE"Submit">
    </FORM>
    <br>
    Combination<br> <br>


    it's just that no headers should be send before session start.........


    Nico wrote:
    Dear all,
    >
    I made the following form.
    >
    <FORM METHOD="POST" ACTION="prova.p hp">
    <b>Combinatio n</b><br><br>
    Element 1: <INPUT NAME="el1" TYPE="TEXT">
    <BR>
    Element 2: <INPUT NAME="el2" TYPE="TEXT">
    <br>
    <INPUT TYPE="SUBMIT" VALUE"Submit">
    </FORM>
    <br>
    Combination<br> <br>
    >
    <?php
    session_start() ;
    $_SESSION['combinations'][] = ($_REQUEST["el1"].$_REQUEST["el2"]);
    print_r($_SESSI ON['combinations']);
    ?>
    >
    If I fill the two text fields, reload the page and then fill again the
    two text fields, the new text overwrites the old one.
    I'd like that the array can include both the old and the new text.
    I tried to use the sessions, but it doesn't work.
    Can you help me in solving this problem please?
    Many thanks.
    >
    Cheers,
    Nico

    Comment

    • Nico

      #3
      Re: Sessions

      Thanks. I tried your proposal but the result is the same.
      So, the new inserted text overwrites the old one.
      Other suggestions? Many thanks.
      Cheers,
      Nico

      In article <1165752223.583 674.156030@l12g 2000cwl.googleg roups.com>,
      "theCanceru s" <thecancerus@gm ail.comwrote:
      simple do this
      <?php
      session_start() ;
      $_SESSION['combinations'][] = ($_REQUEST["el1"].$_REQUEST["el2"]);
      print_r($_SESSI ON['combinations']);
      ?>
      <FORM METHOD="POST" ACTION="aktry.p hp">
      <b>Combinatio n</b><br><br>
      Element 1: <INPUT NAME="el1" TYPE="TEXT">
      <BR>
      Element 2: <INPUT NAME="el2" TYPE="TEXT">
      <br>
      <INPUT TYPE="SUBMIT" VALUE"Submit">
      </FORM>
      <br>
      Combination<br> <br>
      >
      >
      it's just that no headers should be send before session start.........
      >
      >
      Nico wrote:
      Dear all,

      I made the following form.

      <FORM METHOD="POST" ACTION="prova.p hp">
      <b>Combinatio n</b><br><br>
      Element 1: <INPUT NAME="el1" TYPE="TEXT">
      <BR>
      Element 2: <INPUT NAME="el2" TYPE="TEXT">
      <br>
      <INPUT TYPE="SUBMIT" VALUE"Submit">
      </FORM>
      <br>
      Combination<br> <br>

      <?php
      session_start() ;
      $_SESSION['combinations'][] = ($_REQUEST["el1"].$_REQUEST["el2"]);
      print_r($_SESSI ON['combinations']);
      ?>

      If I fill the two text fields, reload the page and then fill again the
      two text fields, the new text overwrites the old one.
      I'd like that the array can include both the old and the new text.
      I tried to use the sessions, but it doesn't work.
      Can you help me in solving this problem please?
      Many thanks.

      Cheers,
      Nico

      Comment

      • alo.and@gmail.com

        #4
        Re: Sessions

        Hi Nico.
        >
        <FORM METHOD="POST" ACTION="prova.p hp">
        <b>Combinatio n</b><br><br>
        Element 1: <INPUT NAME="el1" TYPE="TEXT">
        <BR>
        Element 2: <INPUT NAME="el2" TYPE="TEXT">
        <br>
        <INPUT TYPE="SUBMIT" VALUE"Submit">
        </FORM>
        <br>
        Combination<br> <br>
        >
        <?php
        session_start() ;
        $_SESSION['combinations'][] = ($_REQUEST["el1"].$_REQUEST["el2"]);
        print_r($_SESSI ON['combinations']);
        ?>
        >
        First, session_start() must be called before any output if you use
        Cookie based sessions (the majority of PHP implementations use this
        configuration). See
        http://www.php.net/manual/en/function.session-start.php for more info.

        Then your script should be like this:

        ---file begin---
        <?php

        session_start() ;

        ?>
        <p>Do some output here. <?php echo "Other output here."; ?></p>
        <p>Etc.</p>
        <?php

        // Write something to the _SESSION array
        $_SESSION['something'] = "something that sould be saved in session";

        ?>
        ---file end---

        Doing that, your data contained in $_SESSION array will be saved
        between browser requests.
        If I fill the two text fields, reload the page and then fill again the
        two text fields, the new text overwrites the old one.
        If you just reload the page the browser will not send any data filled
        in forms. To do that you should submit your form, via input with
        type="submit" or type="image" or via document.formNa me.submit()
        javascript method.
        I'd like that the array can include both the old and the new text.
        You're doing right in this statement:

        $_SESSION['combinations'][] = ($_REQUEST["el1"].$_REQUEST["el2"]);

        But it could be a bit better, you could save your form data in a more
        normalized way, like a form submit history, I guess you want to do
        something like that:

        $_SESSION['form_history'][] = $_REQUEST;

        So, to get the last form post, for example, you could do this way:

        $last_request = $_SESSION['form_history'][
        count($_SESSION['form_history']) - 1 ];
        print_r($last_r equest);
        echo "The last posted value of Element 1 was " .
        $last_request['el'];

        Note: the example statements was not tested.

        Hope I can help you!

        Comment

        • theCancerus

          #5
          Re: Sessions

          hi Nico
          the example i provided to you works but for it to work you must "submit
          your form, via input with
          type="submit" as pointed above....
          also replace aktry.php in my example with name of the file in which the
          code was saved

          it will work with out fail.. in case it does not the canyou show me the
          exact out put what it dispalys

          On Dec 10, 5:18 pm, alo....@gmail.c om wrote:
          Hi Nico.
          >
          >
          >
          >
          >
          <FORM METHOD="POST" ACTION="prova.p hp">
          <b>Combinatio n</b><br><br>
          Element 1: <INPUT NAME="el1" TYPE="TEXT">
          <BR>
          Element 2: <INPUT NAME="el2" TYPE="TEXT">
          <br>
          <INPUT TYPE="SUBMIT" VALUE"Submit">
          </FORM>
          <br>
          Combination<br> <br>
          >
          <?php
          session_start() ;
          $_SESSION['combinations'][] = ($_REQUEST["el1"].$_REQUEST["el2"]);
          print_r($_SESSI ON['combinations']);
          ?>First, session_start() must be called before any output if you use
          Cookie based sessions (the majority of PHP implementations use this
          configuration). Seehttp://www.php.net/manual/en/function.sessio n-start.phpfor more info.
          >
          Then your script should be like this:
          >
          ---file begin---
          <?php
          >
          session_start() ;
          >
          ?>
          <p>Do some output here. <?php echo "Other output here."; ?></p>
          <p>Etc.</p>
          <?php
          >
          // Write something to the _SESSION array
          $_SESSION['something'] = "something that sould be saved in session";
          >
          ?>
          ---file end---
          >
          Doing that, your data contained in $_SESSION array will be saved
          between browser requests.
          >
          If I fill the two text fields, reload the page and then fill again the
          two text fields, the new text overwrites the old one.If you just reload the page the browser will not send any data filled
          in forms. To do that you should submit your form, via input with
          type="submit" or type="image" or via document.formNa me.submit()
          javascript method.
          >
          I'd like that the array can include both the old and the new text.You're doing right in this statement:
          >
          $_SESSION['combinations'][] = ($_REQUEST["el1"].$_REQUEST["el2"]);
          >
          But it could be a bit better, you could save your form data in a more
          normalized way, like a form submit history, I guess you want to do
          something like that:
          >
          $_SESSION['form_history'][] = $_REQUEST;
          >
          So, to get the last form post, for example, you could do this way:
          >
          $last_request = $_SESSION['form_history'][
          count($_SESSION['form_history']) - 1 ];
          print_r($last_r equest);
          echo "The last posted value of Element 1 was " .
          $last_request['el'];
          >
          Note: the example statements was not tested.
          >
          Hope I can help you!

          Comment

          • Nico

            #6
            Re: Sessions

            Hi,

            Actually it's quite strange.
            I know it's correct, but it doesn't work.
            I changed all the necessary things (e.g. aktry.php...)
            I suppose to have a problem with session in my localhost.
            If I try the example related to sessions and included in the official
            php manual:

            <?php
            // page1.php

            session_start() ;

            echo 'Welcome to page #1';

            $_SESSION['favcolor'] = 'green';
            $_SESSION['animal'] = 'cat';
            $_SESSION['time'] = time();

            // Works if session cookie was accepted
            echo '<br /><a href="page2.php ">page 2</a>';

            // Or maybe pass along the session id, if needed
            echo '<br /><a href="page2.php ?' . SID . '">page 2</a>';
            ?>

            <?php
            // page2.php

            session_start() ;

            echo 'Welcome to page #2<br />';

            echo $_SESSION['favcolor']; // green
            echo $_SESSION['animal']; // cat
            echo date('Y m d H:i:s', $_SESSION['time']);

            // You may want to use SID here, like we did in page1.php
            echo '<br /><a href="page1.php ">page 1</a>';
            ?>

            I've the following result:

            "Welcome to page #1
            page 2
            page 2"

            If I visit the first link:

            "Welcome to page #2
            1970 01 01 01:00:00
            page 1"

            If I vist the second link:

            "Welcome to page #2
            greencat2006 12 10 21:24:02
            page 1"

            It seems that in the first case sessions don't work.
            Why? I'm not able to understand.
            Can you help me please?
            Should I change something in my php.ini or httpd.conf?
            Many thanks.

            Cheers,
            Nico

            In article <1165780715.458 164.109920@16g2 000cwy.googlegr oups.com>,
            "theCanceru s" <thecancerus@gm ail.comwrote:
            hi Nico
            the example i provided to you works but for it to work you must "submit
            your form, via input with
            type="submit" as pointed above....
            >
            also replace aktry.php in my example with name of the file in which the
            code was saved
            >
            it will work with out fail.. in case it does not the canyou show me the
            exact out put what it dispalys
            >

            Comment

            • theCancerus

              #7
              Re: Sessions

              hello Nico,

              i think you have disabled the cookies in you browser... that is one
              reason why session works with SID but not without. For session to work
              properly session id is by default saved as cookies.

              i can't think of any other reason... other then their is also a bug in
              PHP where in session sometimes does not work and works other times.. so
              my suggestion if cookies are all fine then use SID.

              cheers
              Amit



              On Dec 11, 1:24 am, Nico <noem...@noemai l.comwrote:
              Hi,
              >
              Actually it's quite strange.
              I know it's correct, but it doesn't work.
              I changed all the necessary things (e.g. aktry.php...)
              I suppose to have a problem with session in my localhost.
              If I try the example related to sessions and included in the official
              php manual:
              >
              <?php
              // page1.php
              >
              session_start() ;
              >
              echo 'Welcome to page #1';
              >
              $_SESSION['favcolor'] = 'green';
              $_SESSION['animal'] = 'cat';
              $_SESSION['time'] = time();
              >
              // Works if session cookie was accepted
              echo '<br /><a href="page2.php ">page 2</a>';
              >
              // Or maybe pass along the session id, if needed
              echo '<br /><a href="page2.php ?' . SID . '">page 2</a>';
              ?>
              >
              <?php
              // page2.php
              >
              session_start() ;
              >
              echo 'Welcome to page #2<br />';
              >
              echo $_SESSION['favcolor']; // green
              echo $_SESSION['animal']; // cat
              echo date('Y m d H:i:s', $_SESSION['time']);
              >
              // You may want to use SID here, like we did in page1.php
              echo '<br /><a href="page1.php ">page 1</a>';
              ?>
              >
              I've the following result:
              >
              "Welcome to page #1
              page 2
              page 2"
              >
              If I visit the first link:
              >
              "Welcome to page #2
              1970 01 01 01:00:00
              page 1"
              >
              If I vist the second link:
              >
              "Welcome to page #2
              greencat2006 12 10 21:24:02
              page 1"
              >
              It seems that in the first case sessions don't work.
              Why? I'm not able to understand.
              Can you help me please?
              Should I change something in my php.ini or httpd.conf?
              Many thanks.
              >
              Cheers,
              Nico
              >
              In article <1165780715.458 164.109...@16g2 000cwy.googlegr oups.com>,
              >
              "theCanceru s" <thecance...@gm ail.comwrote:
              hi Nico
              the example i provided to you works but for it to work you must "submit
              your form, via input with
              type="submit" as pointed above....
              >
              also replace aktry.php in my example with name of the file in which the
              code was saved
              >
              it will work with out fail.. in case it does not the canyou show me the
              exact out put what it dispalys

              Comment

              • Nico

                #8
                Re: Sessions

                Many thanks Amit.
                Actually I activated cookies.
                What is strange is that two days ago everything went perfectly.
                Can you help me please in using SID?
                There is a particular code I should write?
                Many thanks.

                Cheers,
                Nico

                Comment

                Working...