简体   繁体   中英

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

<?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?

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".

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();
        }
    ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM