Undefined variable: _SESSION can someone help me with this thanks

i keep getting the error Undefined variable: _SESSION and don’t understand why could someone help me the start session is included in header.php

include ‘mysql.php’;

//the user has admin rights

$name = isset($_POST[‘name’]) ? trim($_POST[‘name’]) : ‘’;
$description = isset($_POST[‘description’]) ? trim($_POST[‘description’]) : ‘’;

//Create variable to hold error message
$errorMsg = ‘’;
if($_SESSION[‘loggedIn’] == false | $_SESSION[‘user_level’] != 1 )
//the user is not an admin
echo ‘Sorry, you do not have sufficient rights to access this page.’;
//Check if form was posted

header.php is the following
if ($_SESSION[‘loggedIn’])
echo 'Hello ’ . htmlentities($_SESSION[‘username’]) . ‘. Not you? Sign out’;
echo ‘Sign in or create an account’;

Maybe the below will help?

[php]if (isset($_SESSION[‘user’])) { // check session

}} else { // redirect to login page
header(‘Location: index.php’);

I know I have an extra { bracket ;D

Oh, you probably know but in case you don’t know any redirect has to come before any header information is sent…maybe that is another part of your problem with you other code? Myself that is one of the first things I do before continuing on with coding is checking an users login status. Saves a lot of headaches. :smiley:

Thats not the cause of this error though. php is looking at the variable before its being assigned. Either set it as empty at the top of the page or wrap the vatiable in an of statement.

Sponsor our Newsletter | Privacy Policy | Terms of Service