/    Sign up×
Community /Pin to ProfileBookmark

unable to change value of $_SESSION

Hi I’m trying to use a session variable in my php script.

Every page starts with

[code=php]<?php include(‘include/header-section.php’)?>[/code]

which starts:

[CODE]
<?php session_start(); ?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”><html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en-GB”>
<head>
[/CODE]

Every page has

[code=php]<?php include(‘include/menu.php’) ?>[/code]

which includes this link for a contact form:

[code=html]
<li class=”accordionButton”><a class=”rfpMenuBtn” href=”contactform.php?id=1″
<?php $_SESSION[‘contactform_id’]=1 ?> >Contact us</a></li>
[/code]

Every page has

[code=php]<?php include(‘include/footer.php’); ?> [/code]

which contains links to the same contact form

[code=html]
<div class=”push”></div>
<div class=”footer”>
<div style=”width: 23%; float: left;”>Moshe Avni – Art Work.</div>
<div style=”width: 53%;text-align: center; float: left;”>
<a href=”contactform.php?id=2″
<?php $_SESSION[‘contactform_id’]=2 ?> >Rahel Warshaw-Dadon – Reiki Master. Email: [email protected]</a>
</div>
<div style=”width: 23%; float: right; text-align: right;”>
<a href=”contactform.php?id=3″
<?php $_SESSION[‘contactform_id’]=3 ?>>David Myers – Web Master.</a>

</div>

</div>

[/code]

contactform.php also includes menu.php and footer.php if that’s relevant.

The problem is I cannot change the value of $_SESSION[‘contactform_id’], it is always the value that’s set in menu.php (1), and clicking on links in footer.php (2 or 3) do not change the value.
Sorry about including all this code but I have no idea where the problem is.
Thanks for any help

David

“paddling upstream searching for the source”

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 26.2011 — Not sure, but I think there may be a logic/control-flow issue here. It looks like each place where you create a link with an "?id=n" in the URL, you are also setting $_SESSION['contactform_id'] to "n". This means that $_SESSION will always be set to whichever of those pieces of link coding was executed last before you look at the $_SESSION['contactform_id'] value. I'm not sure what you want to do with that $_SESSION value, but it would appear to me that perhaps you would actually be more interested in $_GET['id'], which would tell you which one of those links was clicked to call the current page. If you also want to save that in $_SESSION for some reason, then I [b]think[/b] maybe all you need is to remove those separate lines that are setting $_SESSION['contactform_id'] with one line somewhere prior to the point where you need to know what it's value is (and after session_start() has been done):

[code=php]
$_SESSION['contactform_id'] = (isset($_GET['id'])) ? $_GET['id'] : 0;
[/code]

Change the zero at the end to whatever default value you want to use if $_GET['id'] is not set (e.g. null or false if that makes more sense).
×

Success!

Help @davidM_ME 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.18,
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,
)...