0

I'm working on a Tenders script right now, and I'm doing very well so far, it all works, but I can't get this to get away, I'm not really sure how. i got this error

PHP Notice: Undefined index: loggedIn in /home/monaqs83/public_html/util/Auth.php on line 11

and this the Auth.php please can you help me solve this problem

<?php
/**
 * 
 */
class Auth
{

    public static function handleLogin()
    {
        @session_start();
        $logged = $_SESSION['loggedIn'];
        if ($logged == false) {
            session_destroy();
            header('location: ../login');
            exit;
        }
    }
    public static function handleRole()
    {
        $roleArray = array(
            'directorates',
            'publisher',
            'user',
            'type',
            'subcategory',
            'category'
        );
        $url  = $_SERVER['REQUEST_URI']; $url = explode('/', $url);
        if($_SESSION['role'] == 1)
        if(in_array($url[0], $roleArray))
            header('Location: '.URL.'dashboard');
    } 
}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Chakerfrh
  • 1
  • 1
  • 2
    You'll need to check first if $_SESSION['loggedIn'] exists or not – mehulmpt Mar 04 '17 at 08:13
  • 1
    What does the error tell you? It is crystal clear in what is says: `$_SESSION` does not contain an element with name `loggedIn` which you try to access in that line... – arkascha Mar 04 '17 at 08:14
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – John V. Mar 04 '17 at 08:54

3 Answers3

2

loggedInkey does not exists in your $_SESSION variable.

Check if it exists with isset keyword

$logged = isset($_SESSION['loggedIn']) ? $_SESSION['loggedIn'] : false;
unalignedmemoryaccess
  • 7,246
  • 2
  • 25
  • 40
1

change $logged = $_SESSION['loggedIn']; to $logged = !empty($_SESSION['loggedIn']) && $_SESSION['loggedIn'];

Nir O.
  • 1,563
  • 1
  • 17
  • 26
0

Instead of directly accessing it like $logged = $_SESSION['loggedIn'];, use this:

$logged = false;
if(isset($_SESSION['loggedIn'])) {
$logged = $_SESSION['loggedIn'];
}
mehulmpt
  • 15,861
  • 12
  • 48
  • 88