473,545 Members | 2,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Struggling with session variables

Some time ago I already posted a question about a site that consists of
several pages where the visitor, on every page can answer one or two
questions in order to fill out an intake for for an investment advice.
So in the page question1.php = a form "action = question2.php" and in
question2.php the variables (results of the form) are registered to the
session.
What I still didn't succeed to is the following: let us say the customer is
filling out the question5.php form but suddenly realises he made a mistake
in question2.php and wants to navigate back. Up to now I didn't succeed to
enable the customer to change the value in question2.php , when it has been
set once.
I already got the suggestion that rather than
//session_registe r("someVariable ");

I should do something like

$session_id = session_id();
if(isset($someV ariable)) {
$_SESSION['someVariable'] = $someVariable;
}
else {
$someVariable = $_SESSION['someVariable'];
}
But it still doesn't resolve the problem I described above. Whatever I try
to search in Google, I cannot find any answers so a link to a page that
gives hints and tips would be just as welcome as an answer to the question
itself
Thanks,

Martien.
Jul 17 '05 #1
5 1997
Martien van Wanrooij wrote:
What I still didn't succeed to is the following: let us say the customer is
filling out the question5.php form but suddenly realises he made a mistake
in question2.php and wants to navigate back. Up to now I didn't succeed to
enable the customer to change the value in question2.php , when it has been
set once.
I already got the suggestion that rather than
//session_registe r("someVariable ");

I should do something like

$session_id = session_id();
if(isset($someV ariable)) {
$_SESSION['someVariable'] = $someVariable;
}
else {
$someVariable = $_SESSION['someVariable'];
}

Just start the session and use
$_SESSION['variable']
as another variable that keeps its value between pages.

Imagine
$_SESSION['variable']
is just a "funny" name for
$SESSION_variab le
and use it whenever you need.

Ok, you have to save the information between all the pages. I use
session variables for that. The POST data is put in a session variable,
which is waht is used to display back to the user.
#v+
<?php // text1.php
session_start() ;

// avoid notices when going forward
$text1 = (isset($_SESSIO N['text1'])) ? $_SESSION['text1'] : '';
echo <<<HTML
<head><title>te xt1</title></head><body>
<form method="post" action="text2.p hp">
text1: <input type="text" name="text1" value="$text1"/><br/>
<input type="submit"/>
</form></body></html>
HTML;
?>

<?php // text2.php
session_start() ;
// save POST data in a session variable
$_SESSION['text1'] = (isset($_POST['text1'])) ? $_POST['text1'] : '';

// if there already is text2 data in session use it
$text2 = (isset($_SESSIO N['text2'])) ? $_SESSION['text2'] : '';
echo <<<HTML
<head><title>te xt2</title></head><body>
<form method="post" action="text3.p hp">
text2: <input type="text" name="text2" value="$text2"/><br/>
<input type="submit"/>
</form><p>Return to <a href="text1.php ">text1</a>.</p>
</body></html>
HTML;
?>

<?php // text3.php
session_start() ;
// save POST data
$_SESSION['text2'] = (isset($_POST['text2'])) ? $_POST['text2'] : '';

$text3 = (isset($_SESSIO N['text3'])) ? $_SESSION['text3'] : '';
echo <<<HTML
<head><title>te xt3</title></head><body>
<form method="post" action="text4.p hp">
text3: <input type="text" name="text3" value="$text3"/><br/>
<input type="submit"/>
</form><p>Return to <a href="text1.php ">text1</a> or <a
href="text2.php ">text2</a>.</p>
</body></html>
HTML;
?>

<?php // text4.php
session_start() ;
// save POST data
$_SESSION['text3'] = (isset($_POST['text3'])) ? $_POST['text3'] : '';

echo <<<HTML
<head><title>te xt4</title></head><body>
<p>You entered:<br/>
text1: {$_SESSION['text1']}<br/>
text2: {$_SESSION['text2']}<br/>
text3: {$_SESSION['text3']}<br/>
</p>
<p>Return to <a href="text1.php ">text1</a> or
<a href="text2.php ">text2</a> or
<a href="text3.php ">text3</a>.</p>
</body></html>
HTML;
?>
#v-
Happy Coding :-)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2
Thank you very much Pedro, I haven''t got the time yet to study the whole
code but this _must_ be the solution :-)
Jul 17 '05 #3
Martien van Wanrooij wrote:
Thank you very much Pedro, I haven''t got the time yet to study the whole
code but this _must_ be the solution :-)


Well ... it needs some tweaking :)
I posted it without comprehensive testing.

The way I posted it allows the session variables to be overwritten with
'' (the blank string) when you return to a previous page.

The culprit is

$_SESSION['textN'] = (if isset($_POST['textN'])) ? $_POST['textN'] : '';
After my /late/ testing I'd change those lines to

if (isset($_POST['textN'])) $_SESSION['textN'] = $_POST['textN'];

which doesn't clear the session variable for a GET.

In real life, for scripts that work for GETs and POSTs, I always do

if ($_SERVER['REQUEST_METHOD '] == 'POST') {
// deal with POST data
}

and that bug would be gone inside the if block.
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #4
On Mon, 02 Feb 2004 22:33:49 +0000, Pedro Graca wrote:
In real life, for scripts that work for GETs and POSTs, I always do

if ($_SERVER['REQUEST_METHOD '] == 'POST') {
// deal with POST data
}
}
and that bug would be gone inside the if block.


Why not use $_REQUEST instead? GET and POST both go into $_REQUEST.

I used to use

${'HTTP_' . $REQUEST_METHOD . '_VARS'}
but
$_REQUEST
is the same thing.

--
Jeffrey D. Silverman | jeffrey AT jhu DOT edu
Website | http://www.wse.jhu.edu/newtnotes/

Jul 17 '05 #5
Jeffrey Silverman wrote:
In real life, for scripts that work for GETs and POSTs, I always do

if ($_SERVER['REQUEST_METHOD '] == 'POST') {
// deal with POST data
}
and that bug would be gone inside the if block.


Why not use $_REQUEST instead? GET and POST both go into $_REQUEST.

I used to use

${'HTTP_' . $REQUEST_METHOD . '_VARS'}
but
$_REQUEST
is the same thing.


Well, I once did a combined GET/POST :-)

<form action="combine d.php?data=4">
Data: <input type="text" name="data" value="four"/>
<input type="submit"/>
</form>

and in combined.php I could tell which was which :)

if ($_POST['data'] == 'four') { /* whatever */ }
if ($_GET['data'] == '4') { /* whatever */ }
Since then, for me, $_POST is *very* different than $_GET
and they are treated separately
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2371
by: Al Jones | last post by:
This is a repost form the vbscript newgroup - if this isn't the appropriate group would you point me toward one that is. Basically, I seem to be losing session data part way though preparing an email from (possibly) three seperate forms. the following code is the end of a routine which stashes data from the first form off to session...
6
656
by: Lina Manjarres | last post by:
Hello, I have a session variable in a login page. Then I go to a form page where I uses the ProfileID and the UserID. Then I go to a result page where I would like to use the UserID as a filter, but I can't get the value is stored in it. How can I do that? Thanks a lot!
4
5577
by: PJ | last post by:
A particular page seems to be having issues with correctly setting Session variables. I am setting a couple of session variables on the Page_Unload event. While stepping through code, the immediate window will show the values in Session after the relevant lines that set the variables in the Page_Unload event. However, on postback, these...
31
6970
by: Harry Simpson | last post by:
I've come from the old ASP camp where session variables were not used. When i started using ASP.NET in 2001, I started using them again because it was ok from what I'd read. I've been merrily using Session variables for three years now and i'm entering a project with my new boss who has never quite come around that session variables are...
10
3489
by: tshad | last post by:
I have been using the default session state (InProc) and have found that I have been loosing my information after a period of time (normally 20 minutes). Is there anyway to find out how much more time I have on a session? If I do a refresh, does reset the session clock? Do you have have to go to another page to reset the session timeout...
3
2891
by: Alan Wang | last post by:
Hi there, Once my application gets complicated and complicated. I found it's really hard to keep track of Session value I am using in my asp.net application. I am just wondering if anyone have any experience on how to keep track of session value. Any help it's appreciated. Thanks Alan
3
2667
by: Phillip N Rounds | last post by:
I'm writing a user control which has two states: Active & InActive. I additionally am required that there to be only one active control per page, and all logic has to be contained within the control. In its inactive state, only a single button appears. If the user clicks on this button, the control becomes active( the rest of the...
18
3416
by: BillE | last post by:
When a user opens a new IE browser window using File-New-Window the integrity of an application which relies on session state is COMPLETELY undermined. Anyone who overlooks the fact that File-New-Window creates an instance of IE in the same process with the same SessionID as the parent window is in big trouble. This fundamentally restricts...
26
3585
by: BillE | last post by:
Some ASP.NET applications use Session Variables extensively to maintain state. These should be re-written to use viewstate, hidden fields, querystring, etc. instead. This is because if a user opens a new IE window with Ctrl-N or File-New-Window, BOTH WINDOWS SHARE THE SAME SESSION VARIABLES. This cannot be prevented.
0
7425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7780
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5351
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5069
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3479
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3465
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.