2012-08-03 32 views
0

我想讓代碼顯示錯誤信息,同時刪除頁面內容php顯示錯誤信息和清理內容

EG。

<? 
echo 'welcome'; 
if (login==0) 
{ 
    error("you can't access to this page"); 
} 
content, content, content, content, content, content, content, content, content, content 
ect....... 
?> 

輸出

you can't access to this page 

的爲例刪除所有內容,但保留錯誤(「你不能訪問此頁」);

+0

使用輸出緩衝:http://en.php.net/manual/en/ref。 outcontrol.php – kirilloid 2012-08-03 20:36:45

回答

1

在您的if語句中添加exit();。使用你的例子:

<? 
echo 'welcome'; 
if (login==0) 
{ 
    error("you can't access to this page"); 
    exit(); 
} 
content, content, content, content, content, content, content, content, content, content 
ect....... 
?> 
+0

馬特哈維的答案是最簡單的解決方案。您可能還想在if(login ==「0」)條件檢查後移動echo「welcome」,以便您看不到歡迎消息,然後出現「您無權訪問此頁面」消息 – raidenace 2012-08-03 20:40:43

+0

我想要清除所有輸出並只保留「你不能訪問此頁面」 – 2012-08-03 20:42:24

+0

是的,只要將'echo'welcome';'移到關閉if語句的'}'之後。否則,在輸出錯誤之前,您需要使用Javascript來清除頁面內容。另外,你可以改變'error(「...」);'簡單地'回顯'錯誤信息。 – 2012-08-03 21:13:11

0

你可以使用什麼馬特提供爲好,這裏是另一種選擇:

<? 
echo 'welcome'; 
if(login==0) { 
    error("you can't access to this page"); 
} else { 
?> 
content, content, content, content, content, content, content, content, content, content 
<?php 
} 
?>