Page 1 of 2
sessions problem
Posted: Thu Aug 03, 2006 11:18 am
by nhan
Hi, this is weird, i have created a session that will pass variables to another page, i used the code below :
i used <form action='$_SERVER[PHP_SELF]' method='post'> as the form action
page 1
Code: Select all
session_start();
$_SESSION['userName'] = $_POST['userName'];
$_SESSION['group'] = $_POST['group'];
$_SESSION['process'] = $_POST['process'];
page 2
Code: Select all
session_start();
$name = $_SESSION['userName'];
$group = $_SESSION['group'];
$process= $_SESSION['process'];
print $name;
print $group;
print $process;
when i open the browser, username was not displayed but when i click back button on the browser and submit again, username and other info was displayed....
is there anyway to solve this problem? thanks a lot!
Posted: Thu Aug 03, 2006 11:20 am
by Benjamin
Closing a browser kills a session.
Posted: Thu Aug 03, 2006 11:29 am
by nhan
the values of username, group and process comes from a form field which a user enters the data, what i mean is when the user submits the data on the form using page 1, this is when i opened IE for the first time, the values of username, group and process was not displayed..... but when i click the back button and enter the same data... those info were displayed.....
Posted: Thu Aug 03, 2006 11:32 am
by Benjamin
What page are you posting data to?
Posted: Thu Aug 03, 2006 11:34 am
by nhan
on the second page
Posted: Thu Aug 03, 2006 11:36 am
by Benjamin
You gotta set the sessions on the second page..
Code: Select all
$_SESSION['userName'] = $_POST['userName'];
$_SESSION['group'] = $_POST['group'];
$_SESSION['process'] = $_POST['process'];
Posted: Thu Aug 03, 2006 11:41 am
by Benjamin
You do realize that you don't need to use sessions if your only sending it to the second page right? This would also work..
Code: Select all
echo $_POST['name'];
echo $_POST['group'];
echo $_POST['process'];
Posted: Thu Aug 03, 2006 11:48 am
by nhan
still the same now when i added the code, values were not displayed even i click the back button and submit the same data...

Posted: Thu Aug 03, 2006 11:50 am
by Benjamin
Your going to have to post your code. We are going to have to move this over to PHP Code category as well.
Posted: Thu Aug 03, 2006 11:52 am
by nhan
ok, ill show the code... should i reply still on this thread....? This is really wierd actually, i have already made this thing working.. i really cant explain why it has gone wrong...
Posted: Thu Aug 03, 2006 11:56 am
by Benjamin
Yes
Posted: Thu Aug 03, 2006 12:01 pm
by nhan
heres the code...
index.php
Code: Select all
$id = session_id();
$result = mssql_query("SELECT count(userId) As counter FROM users WHERE userPass='$_POST[userPass]' AND userName='$_POST[userName]'") or die("Couldn't query the user-database.");
$num = mssql_result($result, 0, counter);
if (!$num) {
echo " [html with code..... i will just paste the important ones... ]
<form action='$_SERVER[PHP_SELF]' method='post'>
<input type='text' name='userName' value='$_POST[userName]'>";
} else {
session_start();
$userName = $_POST['userName'];
$group = $_POST['group'];
$process = $_POST['process'];
$_SESSION['userName'] = $_POST['userName'];
$_SESSION['group'] = $_POST['group'];
$_SESSION['process'] = $_POST['process'];
header("Location: index2.php?name=$userName&campaign=$group&process=$process&sid=$id");
}
index.php2
Code: Select all
session_start();
$name = $_SESSION['userName'];
$group = $_SESSION['group'];
$process= $_SESSION['process'];
print $name;
print "<BR>";
print $group;
print "<BR>";
print $process;
print "<BR>";
when i open the URL for the first time, the forms will appear, i will enter the data and submit the form, the second page displays nothing.... but when i click back button of IE then re submit the data, those info were now displayed.... thanks so much for the help....
Posted: Thu Aug 03, 2006 12:05 pm
by nhan
actually im not only going to send these variable on the second page, i have to use these data for some query and addition procedures.... i just need these 3 data as inputs from the user... from there i can manipulate some procedures....

Posted: Thu Aug 03, 2006 12:10 pm
by Benjamin
I don't know what the problem is. I would guess it has something to do with you manually setting the sid in the header url.
Might want to test it without &sid=$id..
Posted: Thu Aug 03, 2006 12:15 pm
by nhan
actually i have already done that, i just made the header redirecting to index2.php without the $sid=$id.... but still the ssame, variables are only passed to the next page whenever i click the back button of IE and reenter the same data...
