可能重复:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”为什么我的索引未定义
我尝试创建一个简单的login and register page
,我让我的index .php
网页上的错误,为什么我的索引未定义这些是我得到的错误
Notice: Undefined index: username in C:\xampp\htdocs\mysite\forum part two\index.php on line 7
Notice: Undefined index: password in C:\xampp\htdocs\mysite\forum part two\index.php on line 8
第7和第8行上的代码这些分别
$username = $_SESSION['username'];
$password = $_SESSION['password'];
这是我的索引页
<?php
//This will start a session
session_start();
$username = $_SESSION['username'];
$password = $_SESSION['password'];
//Check do we have username and password
if(!$username && !$password){
echo "Welcome Guest! <br> <a href=login.php>Login</a> | <a href=register.php>Register</a>";
}else{
echo "Welcome ".$username." (<a href=logout.php>Logout</a>)";
}
?>
这只是给一个“通知:未定义的变量”,而不是如果会话的指数没有设置,因为'$ username'和'$ password'如果在会议中不存在,他们永远不会设置。 – animuson
现在这会在用户名和密码在if条件没有设置时抛出未定义的变量错误。首先声明它们是假的。 – dakdad
@dakdad感谢您指出这一点。 – Sibu