/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Recalling Session Variables throughout site

Hey

I cannot seem to recall my session variables throughout my site and i cannot see where i am going wrong.

I have variables in my flash file which i have posted into my ‘info.php’ all working fine, i declare the post variables in ‘info.php’ with the following:

[CODE][B]$num[/B] = $_POST[‘number’];
[B]$choice[/B] = $_POST[‘choice’];
[B]$colour[/B] = $_POST[‘colour’];[/CODE]

These variables then get called in a simple form that is echo’d as below:

[CODE]echo ‘<div id=”info”>
<form name=”form” method=”post” action=””>
<table width=”450″ border=”0″ cellspacing=”1″ cellpadding=”3″>
<tr><td width=”65%”><strong>Number</strong></td>
<td width=”65%”><strong>Choice</strong></td>
<td width=”20%”><strong>Colour</strong></td>
<td width=”15%”><strong>Quantity</strong></td></tr>
<tr><td>[B]’.$num.'[/B]</td><td>[B]’.$choice.'[/B]</td><td><input type=”text” name=”colour” value=”[B]’.$colour.'[/B]”readonly></td>
<td><input type=”text” name=”qty1″ size=”5″ value=”1″> </td></tr>
<tr valign=”bottom”><td><br>Paying with: <br>
<input type=”radio” name=”x” value=”CashPayment” checked=”checked”>Cash<br>
<input type=”radio” name=”x” value=”CardPayment”>Card</td>
<input type=”submit” name=”Submit” value=”Submit”></td></tr>
</table>
</form>’;[/CODE]

I then set the post variables to session variables with the following:

[CODE]$num = $_POST[‘number’];
$choice = $_POST[‘choice’];
$colour = $_POST[‘colour’];
[B]$_SESSION[‘num’][/B] = $_POST[‘number’];
[B]$_SESSION[‘choice’][/B] = $_POST[‘choice’];
[B]$_SESSION[‘colour’][/B] = $_POST[‘colour’];[/CODE]

So the form has the session variables stated as in:

[CODE]echo ‘<div id=”info”>
<form name=”form” method=”post” action=””>
<table width=”450″ border=”0″ cellspacing=”1″ cellpadding=”3″>
<tr><td width=”65%”><strong>Number</strong></td>
<td width=”65%”><strong>Choice</strong></td>
<td width=”20%”><strong>Colour</strong></td>
<td width=”15%”><strong>Quantity</strong></td></tr>
<tr><td>[B]’. $_SESSION[‘num’].'[/B]</td><td>[B]’. $_SESSION[‘choice’].'[/B]</td><td><input type=”text” name=”colour” value=”[B]’. $_SESSION[‘colour’].'[/B]”readonly></td>
<td><input type=”text” name=”qty1″ size=”5″ value=”1″> </td></tr>
<tr valign=”bottom”><td><br>Paying with: <br>
<input type=”radio” name=”x” value=”CashPayment” checked=”checked”>Cash<br>
<input type=”radio” name=”x” value=”CardPayment”>Card</td>
<input type=”submit” name=”Submit” value=”Submit”></td></tr>
</table>
</form>’;[/CODE]

I have

[CODE]<?php
session_start();[/CODE]

At the top of every php page.

Can anyone help me with what i am doing wrong? The variables display when you first go to ‘info.php’ but as soon as you navigate away and then back the fields are empty.

Any ideas?

Many thanks

Jon

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@WebnerdJan 24.2010 — You may need to set some headers to make sure the content is completely fresh.

<i>
</i>header( 'Expires: Sat, 26 Jul 1997 05:00:00 GMT' );
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
header( 'Cache-Control: no-store, no-cache, must-revalidate' );
header( 'Cache-Control: post-check=0, pre-check=0', false );
header( 'Pragma: no-cache' );


Add this after session_start()

Sounds silly but also try also generating a unique form name,

<i>
</i>&lt;form name="form-'.md5(time()).'" method="post" action=""&gt;
Copy linkTweet thisAlerts:
@MindzaiJan 24.2010 — If you navigate away from the page which sets the session variables and then go back to it, unless you also resend the post data the session variables will be empty (since you are setting them to something which doesn't exist). PHP should be giving you an error that you are attempting to use variables (or technically, array indeces) which don't exist, but if you don't have error_reporting turned on and set to a suitable level you won't see them.

Incidentally, directly echoing user input like you are doing here is a dangerous technique which could leave you open to XSS attacks. You should really clean any user input before using it.
Copy linkTweet thisAlerts:
@dk_zero-coolJan 24.2010 — You need to do something like this instead.

[code=php]
if($_SERVER['REQUEST_METHOD'] == "POST") {
$num = $_SESSION['num'] = $_POST['number'];
$choice = $_SESSION['choice'] = $_POST['choice'];
$colour = $_SESSION['colour'] = $_POST['colour'];

}else {
$num = $_SESSION['num'];
$choice = $_SESSION['choice'];
$colour = $_SESSION['colour'];
}
[/code]


Like Mindzai says, every time you enter the page, you overwrite the sessions with null values, unless you have a form submission.
Copy linkTweet thisAlerts:
@oddball25authorJan 25.2010 — dk_zero-cool - thanks for that bit of code, very helpful!!
×

Success!

Help @oddball25 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...