Undefined index: error
i am a beginner in php,i am facing a problem with the following code. it shows undefined index error."Undefined index: username in C:\wamp\www\phpmyadmin_practise\member.php on line 7 you must be login!".i can't understand what shoul 开发者_如何学编程i do.
session_start();
if($_SESSION['username'])
{
echo "Welcome,".$_SESSION['username']."!<br> <a href ='logout.php'> Logout </a> <br> <a href='changepassword.php'> changepassword </a> <br>";
}
else
{
die("you must be login!");
}
?>
username
is not set.
you should check variable with isset function before using them
if(isset($_SESSION['username']))
If you open the page for the first time, $_SESSION['username']
will not be set (the key username
does not exist). But you try to access it, so you get an error.
Use isset
or array_key_exists
:
if(isset($_SESSION['username']))
I hope you also have some kind of login page where the user is actually able to log in. die("you must be login!");
will terminate the current script. If this is the only page you have, $_SESSION['username']
will never be set.
And in production, you should not use die
anyway. Create a proper error message on your page.
$_SESSION['username'] is undefined. try this condition:
if(isset($_SESSION['username']))
精彩评论