2012-09-07 159 views
0

我想隱藏一個頁面的內容使用PHP,但總是得到一個錯誤 這是我正在使用的代碼。從$pass數據來自從其他網頁submited形式:PHP頁面加載錯誤

<?php 
$pass = $_POST['pass']; 
$password = "content"; // Modify Password to suit for access, Max 10 Char. 
?> 

<html> 
<title></title> 
<head> 

<?php 
// If password is valid let the user get access 
if ("$pass" == "$password") { 
?> 

<!-- START OF HIDDEN HTML - PLACE YOUR CONTENT HERE --> 
</head> 
<body> 
You have gained access! 
<!-- END OF HIDDEN HTML --> 

<?php 
} else { 
    // Wrong password or no password entered display this message 
    print "<p align=\"center\"><font color=\"red\"><b>Restricted Area!</b><br>Please enter from the log in page</font></p>";} 
} 
?> 

</body> 
</html> 
+2

什麼是錯誤? –

+0

你會得到什麼錯誤?另外,你爲什麼將變量作爲字符串進行比較?爲什麼不直接比較它們呢? – David

+1

不應該在if()子句中...... – Bgi

回答

0

鏈接,你的代碼沒有運行,結束了一個致命的錯誤,只是因爲你有一個額外的}print後,這意味着你的else結束有一個太多}。刪除它,然後你的代碼運行沒有任何錯誤:

Online Working Example

另外,如果你比較變量,則不得使用引號",試試這樣做:

if ($pass == $password) {

+0

很好,謝謝它現在運行,但我仍然無法查看內容 – Link

+0

我已經更新了我的答案,現在怎麼樣? –

+0

它的作品謝謝你!只是在其他頁面上的表單中輸入錯誤的值,但感謝回覆! – Link

1
print "<p align=\"center\"><font color=\"red\"><b>Restricted Area!</b><br>Please enter from the log in page</font></p>";} 

在這一行上,最後有一個額外的花括號。所以在你的文件中,總體上有一個額外的花括號。 嘗試改變這一行

print "<p align=\"center\"><font color=\"red\"><b>Restricted Area!</b><br>Please enter from the log in page</font></p>"; 

,它應該工作。