identify my registered sessions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dudelideisann
    New Member
    • Jan 2008
    • 12

    identify my registered sessions

    Hi!

    I have a form where the user enters some input. The input will eventionally become a database table. When he hit the 'submit' button the info is put into an array and then into a session variable for later. The session gets it's name from the user inputfield 'tablename' ..

    Then, if the user wants to he can then proceed to 'checkout.php' or he can make a new input in another form and hit the submit button again. The same will happen again, storing all input into an array and then into a session variable.

    My question is now:
    When I go to another page to print all session variables. How can I identify each session beeing stored, so that the input info will come out the right way in my database...
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    You have the same session for the time that your browser is open, unless you destroy or unset that session. So I am a little confused how you think your session variables will get mixed up?

    I am guessing you're worried that when item 1 is added, it will overwrite item 2? Well you can use arrays inside session variables, so maybe something for you to look into.

    Anyway to answer your question, you can get the session id by using:
    session_id() - returns the session id for the current session. If id is specified, it will replace the current session id.
    You can find more info using google.

    Hope that helps.

    Comment

    • dudelideisann
      New Member
      • Jan 2008
      • 12

      #3
      Originally posted by TheServant
      You have the same session for the time that your browser is open, unless you destroy or unset that session. So I am a little confused how you think your session variables will get mixed up?

      I am guessing you're worried that when item 1 is added, it will overwrite item 2? Well you can use arrays inside session variables, so maybe something for you to look into.

      Anyway to answer your question, you can get the session id by using:
      session_id() - returns the session id for the current session. If id is specified, it will replace the current session id.
      You can find more info using google.

      Hope that helps.
      Hi!

      Thanks for your answer. I think I already operate with arrays inside session variable, but I keep overwrite it. Here is what happens when the user click submit button:


      [PHP]

      if($_POST['putIntoArrayBt n']) { // 'putIntoArrayBt n' is my submit button

      $name = $_POST['tableName'];
      $fieldname = $_POST['fieldName']; //fetch all values in name='fieldName[]'
      $fieldvalue = $_POST['fieldValue']; //fetch all values in name='fieldValu e[]'

      $_SESSION['mySession] = array("Tablenam e"=>$name, "Subject"=>$fie ldname,"Value"= >$fieldvalue) ;
      }

      [/PHP]

      But what happens when the user wants to make another input in my form. Then the operation happens again, and overwrites my input in $_SESSION['mySession'].

      When I output the $_SESSION['mySession'] only the last stored info will come...

      Any way around this problem, so that all tables will output calling my session variable ?

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        Originally posted by dudelideisann
        [PHP]$_SESSION['mySession'] = array("Tablenam e"=>$name, "Subject"=>$fie ldname,"Value"= >$fieldvalue) ;[/PHP]
        You didn't have an inverted comma after mySession. But beyond that we need to have a look at your actual form code. I have a feeling that will tell us what we need to know. Please post that as well.

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          Why don't you give each $_SESSION['mySession'] automatically an extra sub-array, so like [php]$_SESSION['mySession'][] = array("Tablenam e"=>$name, "Subject"=>$fie ldname,"Value"= >$fieldvalue) ; [/php](Mind the open-close square brackets after the ['mySession'] key)

          Ronald

          Comment

          • dudelideisann
            New Member
            • Jan 2008
            • 12

            #6
            Originally posted by ronverdonk
            Why don't you give each $_SESSION['mySession'] automatically an extra sub-array, so like [php]$_SESSION['mySession'][] = array("Tablenam e"=>$name, "Subject"=>$fie ldname,"Value"= >$fieldvalue) ; [/php](Mind the open-close square brackets after the ['mySession'] key)

            Ronald

            Hey!

            Thank you! That might be the way to go..
            Would this fetch and output all registered input in my 'checkout.php'? :

            [PHP]print_r($_POST( $_SESSION['mySession'][])); [/PHP]

            Comment

            • ronverdonk
              Recognized Expert Specialist
              • Jul 2006
              • 4259

              #7
              Originally posted by dudelideisann
              Hey!

              Thank you! That might be the way to go..
              Would this fetch and output all registered input in my 'checkout.php'? :

              [PHP]print_r($_POST( $_SESSION['mySession'][])); [/PHP]
              Why the $_POST??? You have stored everything in the $_SESSION array, so
              command[php]print_r($_SESSI ON['mySession']);[/php]will print all arrays and sub-arrays in $_SESSION['mySession'].

              Btw: the [] after an array means to 'assign' the next available entry. So when [0], [1] and [2] are set, it will automatically take [3].

              Ronald

              Comment

              • dudelideisann
                New Member
                • Jan 2008
                • 12

                #8
                Originally posted by ronverdonk
                Why the $_POST??? You have stored everything in the $_SESSION array, so
                command[php]print_r($_SESSI ON['mySession']);[/php]will print all arrays and sub-arrays in $_SESSION['mySession'].

                Btw: the [] after an array means to 'assign' the next available entry. So when [0], [1] and [2] are set, it will automatically take [3].

                Ronald
                hmmm....somethi ng isn't right. Here is all my code for now. Any place I go wrong. Now he only keeps overwriting the session everytime I hit the 'make table' button... The array index always is [0],,, Any thoughts?

                [PHP]if($_POST['myButton']) { //when button is hit

                $name = $_POST['tablename']; //fetched from form in same page
                $fieldname = $_POST['fieldname']; //fetched from form in same page
                $fieldvalue = $_POST['fieldvalue']; // fetched from form in same page


                $_SESSION['mySession'][] = array("Tablenam e"=>$name, "Subject"=>$fie ldname,"Value"= >$fieldvalue) ;

                echo "<table><tr><td ><pre>";

                print_r($_SESSI ON['mySession']);

                echo "</pre></td></tr></table>";[/PHP]




                }

                Comment

                • dudelideisann
                  New Member
                  • Jan 2008
                  • 12

                  #9
                  Originally posted by dudelideisann
                  hmmm....somethi ng isn't right. Here is all my code for now. Any place I go wrong. Now he only keeps overwriting the session everytime I hit the 'make table' button... The array index always is [0],,, Any thoughts?

                  [PHP]if($_POST['myButton']) { //when button is hit

                  $name = $_POST['tablename']; //fetched from form in same page
                  $fieldname = $_POST['fieldname']; //fetched from form in same page
                  $fieldvalue = $_POST['fieldvalue']; // fetched from form in same page


                  $_SESSION['mySession'][] = array("Tablenam e"=>$name, "Subject"=>$fie ldname,"Value"= >$fieldvalue) ;

                  echo "<table><tr><td ><pre>";

                  print_r($_SESSI ON['mySession']);

                  echo "</pre></td></tr></table>";[/PHP]




                  }
                  OOps! Found out! I had put a unset($_SESSION['mySession']) in the start of the page. Now it's ok!

                  Comment

                  • ronverdonk
                    Recognized Expert Specialist
                    • Jul 2006
                    • 4259

                    #10
                    Glad you worked it out! Now if the index increment happens to be lost, you can also use the following solution where the index is also kept in the $_SESSION array, index name 'count'. Name script is A.PHP. If you want, try it and see how it works.[php]<?php
                    session_start() ;
                    if (isset($_POST['submit'])) {
                    if (!isset($_SESSI ON['cnt']))
                    $_SESSION['cnt']=0;
                    $_SESSION['cnt']+=1;
                    $_SESSION['name'][$_SESSION['cnt']]= $_POST['abc'];
                    echo '<pre>'; print_r($_SESSI ON['name']); echo '<br>';
                    }
                    echo 'cnt='.$_SESSIO N['cnt'].'<br>';
                    ?>
                    <form method="post" action="a.php">
                    <input type="text" name='abc'>
                    <input type="submit" name="submit" value="submitit ">
                    </form>[/php]Ronald

                    Comment

                    Working...