Php未定义索引? [重复]

OK so I am making it to where when the user logs out and they try to go back to the member page it says you must be logged in to veiw this page well the echo is displaying that they must be logged in and its keeping them out but there is an error. Here is the page. it says there is a problem on line 5.

MEMBER PHP PAGE

<?php

session_start();

if ($_SESSION['username'])
    echo "Welcome, ".$_SESSION['username']."!<br /><a href='logout.php'>Logout</a>";
else
    die("You must be logged in!");
?>
</div>
<?php

session_start();

if (isset($_SESSION['username']))
echo "Welcome, ".$_SESSION['username']."!<br /><a href='logout.php'>Logout</a>";
else
die("You must be logged in!");
?>

Improve value checking for the if clause. I like to do an array_key_exists as well as a !empty(trim()) to ensure the value is set:

session_start();

if (array_key_exists('username', $_SESSION)) && !empty(trim($_SESSION['username']))) {
    echo "Welcome, " . $_SESSION['username'] . "!<br /><a href='logout.php'>Logout</a>";
}
else {
    die("You must be logged in!");
}

To avoid Undefined Index error, you can use isset() to check :

session_start();
if (isset($_SESSION['username']) && trim($_SESSION['username']) != '') {
    echo 'Welcome, ' . $_SESSION['username'] . '!<br /><a href="logout.php">Logout</a>';
} else {
    die("You must be logged in!");
}

Side note: Assuming these lines are the first few lines of the file, if you print HTML code without HTML head and body, the page is incomplete & browsers may complain about HTML structure. Suggest to have at least minimal HTML structure for every page on screen.