By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,320 Members | 2,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,320 IT Pros & Developers. It's quick & easy.

identify my registered sessions

P: 12
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...
Mar 26 '08 #1
Share this Question
Share on Google+
9 Replies


TheServant
Expert 100+
P: 1,168
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.
Mar 26 '08 #2

P: 12
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 ?
Mar 26 '08 #3

TheServant
Expert 100+
P: 1,168
[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.
Mar 26 '08 #4

ronverdonk
Expert 2.5K+
P: 4,258
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
Mar 27 '08 #5

P: 12
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]
Mar 27 '08 #6

ronverdonk
Expert 2.5K+
P: 4,258
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
Mar 27 '08 #7

P: 12
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]




}
Mar 27 '08 #8

P: 12
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!
Mar 27 '08 #9

ronverdonk
Expert 2.5K+
P: 4,258
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
Mar 27 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.