/    Sign up×
Community /Pin to ProfileBookmark

Can’t get session variables to work!!

Hi, I’m new to sessions. I thought I’d got the idea after reading a few articles, but I just can’t get my code to work!!

As the site I was trying to use them with is complicated enough, I thought I’d try and make a really simple example of what I want to do and I can’t even get that to work…

On one page, I have the code:

[code=php]<?php
session_start();
$_SESSION[‘test’] = ‘hello world’;?>
<head>
<!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”>

<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />
</head>

<body>
<?php echo $_SESSION[‘test’]; ?>
<br />
<a href=”test.php”>test.php</a>
</body>
</html>[/code]

which seems to work fine… it displays the hello world text, etc. But the linked file, when it tries to call the variable, gives the error

[quote]

Notice: Undefined variable: _SESSION in xxxxxx

[/quote]

here’s the code for the second page:

[code=php]<!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”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />
</head>
<body>
<?php
error_reporting(E_ALL);

echo $_SESSION[‘test’]; ?>
</body>
</html>[/code]

I tried it on my localhost and on two different web servers, which just confirmed that it is my code at fault not my server!! Can someone please tell me what I’m doing wrong?
Thanks

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@kloon_zaApr 02.2008 — try starting the session on the other page aswell.
Copy linkTweet thisAlerts:
@mistafeeshauthorApr 02.2008 — dang! I knew it was something simple!! Thanks for the help... that sorted it, both on my simple test page and the more complicated version.

I thought that was just used to begin a session, not introduce it to each page!!!!
Copy linkTweet thisAlerts:
@kloon_zaApr 02.2008 — there is a option you can set in ini file to autotstart sessions.
×

Success!

Help @mistafeesh 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...