Connecting Tech Pros Worldwide Help | Site Map

identify my registered sessions

Newbie
 
Join Date: Jan 2008
Posts: 12
#1: Mar 26 '08
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's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 913
#2: Mar 26 '08

re: identify my registered sessions


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.
Newbie
 
Join Date: Jan 2008
Posts: 12
#3: Mar 26 '08

re: identify my registered sessions


Quote:

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['putIntoArrayBtn']) { // 'putIntoArrayBtn' is my submit button

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

$_SESSION['mySession] = array("Tablename"=>$name, "Subject"=>$fieldname,"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 ?
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 913
#4: Mar 26 '08

re: identify my registered sessions


Quote:

Originally Posted by dudelideisann

[PHP]$_SESSION['mySession'] = array("Tablename"=>$name, "Subject"=>$fieldname,"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.
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#5: Mar 27 '08

re: identify my registered sessions


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

Ronald
Newbie
 
Join Date: Jan 2008
Posts: 12
#6: Mar 27 '08

re: identify my registered sessions


Quote:

Originally Posted by ronverdonk

Why don't you give each $_SESSION['mySession'] automatically an extra sub-array, so like [php]$_SESSION['mySession'][] = array("Tablename"=>$name, "Subject"=>$fieldname,"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]
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#7: Mar 27 '08

re: identify my registered sessions


Quote:

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($_SESSION['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
Newbie
 
Join Date: Jan 2008
Posts: 12
#8: Mar 27 '08

re: identify my registered sessions


Quote:

Originally Posted by ronverdonk

Why the $_POST??? You have stored everything in the $_SESSION array, so
command[php]print_r($_SESSION['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....something 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("Tablename"=>$name, "Subject"=>$fieldname,"Value"=>$fieldvalue);

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

print_r($_SESSION['mySession']);

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




}
Newbie
 
Join Date: Jan 2008
Posts: 12
#9: Mar 27 '08

re: identify my registered sessions


Quote:

Originally Posted by dudelideisann

hmmm....something 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("Tablename"=>$name, "Subject"=>$fieldname,"Value"=>$fieldvalue);

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

print_r($_SESSION['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!
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#10: Mar 27 '08

re: identify my registered sessions


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($_SESSION['cnt']))
$_SESSION['cnt']=0;
$_SESSION['cnt']+=1;
$_SESSION['name'][$_SESSION['cnt']]= $_POST['abc'];
echo '<pre>'; print_r($_SESSION['name']); echo '<br>';
}
echo 'cnt='.$_SESSION['cnt'].'<br>';
?>
<form method="post" action="a.php">
<input type="text" name='abc'>
<input type="submit" name="submit" value="submitit">
</form>[/php]Ronald
Reply