可能重複:
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