PHP Sessions, help needed.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Win32
Forum Newbie
Posts: 2
Joined: Sun Jun 11, 2006 4:15 am

PHP Sessions, help needed.

Post 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
User avatar
ambivalent
Forum Contributor
Posts: 173
Joined: Thu Apr 14, 2005 8:58 pm
Location: Toronto, ON

Post 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"]);

?>
Win32
Forum Newbie
Posts: 2
Joined: Sun Jun 11, 2006 4:15 am

Post 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
Post Reply