Page 1 of 1

PHP Sessions, help needed.

Posted: Sun Jun 11, 2006 4:28 am
by Win32
Hi,
I've been learning HTML/PHP and MySQL over the past three days and have come unstuck on sessions under PHP.
My exact problem is that I receive an error that the "_SESSION" variable is undefined (I'm attempting to implement a login-system for my locally hosted website.)

It seems that the _SESSION variable is only available within the scope of a single PHP script, that or, cookies are not working as they should. For example, take the following two files;

Page1.php

Code: Select all

<SCRIPT language="php">

session_start();

$_SESSION["Username"] = "Win32";

</SCRIPT>
Page2.php

Code: Select all

<SCRIPT language="php">

print($_SESSION["Username"]);

</SCRIPT>
The first script will execute no problem, no errors, but when it comes to the second I will receive the 'undefined _SESSION' error.

Maybe someone could shed some light on my problem? Thanks in advance.

P.S. I'm running an Apache 2.0 webserver and PHP 5.1.4


-Matt

Posted: Sun Jun 11, 2006 8:48 am
by ambivalent
You must call session_start() on each page where you access or write session data.
the manual wrote: Note: Also note that if you are embedding PHP within XML or XHTML you will need to use the <?php ?> tags to remain compliant with standards.

Code: Select all

<?php
session_start();

print($_SESSION["Username"]);

?>

Posted: Mon Jun 12, 2006 9:04 pm
by Win32
You must call session_start() on each page where you access or write session data.
Ah cool thanks alot, that solved my problem.


-Matt