Undefined index

hi all noob again, I am working on a login app, basically when logged in correctly it sets status to ‘on’. I have the below code on all pages I want to protect, it checks if the status is ‘on’ and if its not sends you back to the login page. Login works great, however when hitting a protected page direct and not from the login form I get

Notice: Undefined index line 4. any ideas please? :cool::wink:

<?php
// Starting the session
session_start();
if ($_SESSION[‘status’]!=‘on’)
header(‘Location: login.php’);
?>

cool thanks guys, sorted that one, also is there any difference between operators ‘Or’ and ‘||’ ? I see most php guys using ‘||’ cheers ;):cool:

cool, thanks for good advise here ;):cool:

rajug, if the session is not set, I reckon the OP would also like to show the login page. In that case the code should be


// Starting the session 
session_start();
if (!isset($_SESSION['status']) || $_SESSION['status'] != 'on'){
    header('Location: login.php');
    die();
} 

Check if the session variable ‘staus’ is set or not before you check its value


// Starting the session 
session_start();
if (isset($_SESSION['status']) && $_SESSION['status'] != 'on'){
    header('Location: login.php');
    die();
}

From the manual

So || and “or” do basically the same, but they have different precedences.

Personally I never use the “or” operator. Maybe sometimes in constructs like

$db = mysql_connect(…) or die(…);

But otherwise I stick to || because it’s way easier to spot in your code than “or”.