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

[php]<?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.’;
}
else
{
//Check if form was posted
if($_SERVER[‘REQUEST_METHOD’] == ‘POST’)
{[/php]

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

Maybe the below will help?

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

}} else { // redirect to login page
header(‘Location: index.php’);
exit;
}[/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