0
votes
<?php
if($_SESSION['IS_LOGEDIN'] == 'Y')
{
header("location:index.php?page=home");
exit();
}
?>

I have problem guys, i have this login code and this error when I wanna go in that page.

A session had already been started - ignoring session_start().

I tried to make the code in this way but when I want to go on the page, doesn't want to load:

<?php
   if(!isset($_SESSION['IS_LOGEDIN']) == 'Y')
    { 
        session_start(); 
 header("location:index.php?page=home");
    exit();
    } 
?>

What's the solution?

2
Have you destroyed the session when they log out? - NoLiver92
If you've included session_start(); inside an included/required file, then that could cause it. You should put session_start(); at the top though. Plus this if(!isset($_SESSION['IS_LOGEDIN']) == 'Y') you should be using 2 conditionals, not one. Check if not set, && if equals to. - Funk Forty Niner

2 Answers

2
votes

Your code is flat-out wrong:

if(!isset($_SESSION['IS_LOGEDIN']) == 'Y')
   session_start()

You start your session AFTER you try to test a value that's (presumably) in the session. As well, isset() returns a boolean true/false. It'll never return a string, and it will NOT return whatever value may be set in the variable you're testing. So basically you've got if (true/false == 'Y') which makes no sense.

You probably want something more like this:

session_start()
if (!isset($_SESSION['IS_LOGEDIN']) || ($_SESSION['IS_LOGEDIN'] != 'Y')) {
    header('....');
}

"if the user has never logged in or is not logged in, then redirect".

0
votes

You could try

    <?php
session_start(); //must be at the start of the page
        if(!isset($_SESSION['IS_LOGEDIN']) or empty($_SESSION['IS_LOGEDIN'])) {
        $_SESSION['IS_LOGEDIN'] = 'Y';
        header("location:index.php?page=home");
    exit();
        }
    ?>