Click to See Complete Forum and Search --> : Can't get session variables to work!!


mistafeesh
04-02-2008, 08:02 AM
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:<?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>
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 Notice: Undefined variable: _SESSION in xxxxxx

here's the code for the second page:<!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>

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

kloon_za
04-02-2008, 08:05 AM
try starting the session on the other page aswell.

mistafeesh
04-02-2008, 08:29 AM
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!!!!

kloon_za
04-02-2008, 08:32 AM
there is a option you can set in ini file to autotstart sessions.