2012-03-14 68 views
0

未登錄時,您可以在瀏覽器中鍵入此頁面的URL,並且表單仍顯示。如果沒有登錄,我不希望HTML顯示 - 只是消息說必須登錄。我在其他頁面上使用相同的會話代碼,它的工作原理 - 但確實給出了'未定義索引'這是一個通知有點刺激。有任何想法嗎?PHP會話開始不起作用

<?php 

session_start(); 

if ($_SESSION['first_name']&& $_SESSION['username']) 

echo "Welcome ".$_SESSION['first_name']."<br><a href='login/logged_out.php'>log  
out</a>"; 

else 
die("You must be logged in. Click <a href='login/login_page.php'>here</a> to log  
in."); 

?> 
<html> 
<head> 
</head> 
<body> 
<form id="1" class="rounded" action="test4.php" method="post"/> 
<input type="submit" name="submit" class="button" value="5" /> 

<form id="2" class="rounded" action="test4.php" method="post"/> 
<input type="submit" name="submit" class="button" value="6" /> 

<form id="3" class="rounded" action="test4.php" method="post"/> 
<input type="submit" name="submit" class="button" value="7" /> 

<form id="4" class="rounded" action="test4.php" method="post"/> 
<input type="submit" name="submit" class="button" value="8" /> 

<form id="5" class="rounded" action="test4.php" method="post"/> 
<input type="submit" name="submit" class="button" value="9" /> 

<form id="6" class="rounded" action="test4.php" method="post"/> 
<input type="submit" name="submit" class="button" value="10" /> 

</form> 
</body> 
</html> 
+0

你確定你已經登入嗎? 在調用session_write_close()之前,人們在處理會話時最常犯的錯誤之一是重定向。 – 2012-03-14 23:55:57

+0

[PHP:「Notice:Undefined variable」和「Notice:Undefined index」]的可能重複(http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – animuson 2012-03-14 23:57:57

回答

2

試試這個:

<?php 

session_start(); 

if(isset($_SESSION['first_name']) && isset($_SESSION['username']) && $_SESSION['firstname']!= ''){ 
    echo "Welcome ".$_SESSION['first_name']."<br><a href='login/logged_out.php'>logout</a>"; 
}else{ 
    die("You must be logged in. Click <a href='login/login_page.php'>here</a> to log in."); 
} 
?> 
+0

感謝您的幫助。只是不使用isset()。再次感謝 – user1022772 2012-03-15 22:26:07

1

它並不認爲FIRST_NAME和/或用戶名是在會話指標

FIRST_NAME和/或用戶名檢查是否設置

isset($_SESSION['first_name']) 

isset($_SESSION['username']) 

而且你可能不希望驗證一個基於這些值的布爾條件,除非它們本身是布爾值(儘管first_name不像布爾值那樣讀取)。

0

你的代碼寫得不好。我會修改爲:

<?php 
session_start(); 
if (empty($_SESSION['first_name']) || empty($_SESSION['username'])) { 
    die("You must be logged in. Click <a href='login/login_page.php'>here</a> to log in."); 
} 
echo "Welcome " . $_SESSION['first_name']; 
?> 
<br><a href='login/logged_out.php'>logout</a> 

除非知道它們存在,否則不應該引用數組索引。

0

我不確定什麼是「不開始」的問題。通過檢查isset可以防止未定義的索引。您的代碼還可以通過講變量名,並用括號對的if/else部分大括號改善:

session_start(); 
$isLoggedIn = isset($_SESSION['first_name']) && isset($_SESSION['username']); 
if ($isLoggedIn) 
{ 
    echo "Welcome ", htmlspecialchars($_SESSION['first_name']), 
     "<br><a href='login/logged_out.php'>log out</a>"; 

} 
else 
{ 
    echo "You must be logged in. Click <a href='login/login_page.php'>here</a> to log in."; 
    return; 
} 

這個變種還使用return,而不是die,給後面的程序到更高的實例。

0

與會話工作時,您可以添加的地方

print_r($_SESSION); 

,看看你是否已經設置/取消該會話。