473,385 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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_register("someVariable");

I should do something like

$session_id = session_id();
if(isset($someVariable)) {
$_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 1986
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_register("someVariable");

I should do something like

$session_id = session_id();
if(isset($someVariable)) {
$_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_variable
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($_SESSION['text1'])) ? $_SESSION['text1'] : '';
echo <<<HTML
<head><title>text1</title></head><body>
<form method="post" action="text2.php">
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($_SESSION['text2'])) ? $_SESSION['text2'] : '';
echo <<<HTML
<head><title>text2</title></head><body>
<form method="post" action="text3.php">
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($_SESSION['text3'])) ? $_SESSION['text3'] : '';
echo <<<HTML
<head><title>text3</title></head><body>
<form method="post" action="text4.php">
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>text4</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="combined.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
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...
6
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,...
4
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...
31
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...
10
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...
3
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...
3
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...
18
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...
26
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.