2014-09-23 46 views
-2

如何在找不到用戶時顯示404頁面。
考慮一個網站從$ _GET變量提取用戶名像如何告訴服務器顯示404頁面

example.com/users/?username=johndoe // Normal john doe's profile will be displayed 

example.com/users/?username=jimmy //Should display 404 page without changing url 

如果database.It沒有發現找到的用戶,應該表現出一個404頁。

由於提前

+0

http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php – lordkain 2014-09-23 13:06:46

+0

很瑣事......首先搜索絕地必須 – henser 2014-09-23 13:08:09

回答

0

例子:

header($_SERVER['SERVER_PROTOCOL']." 404 Not Found"); 
echo "Sorry, but user not found :("; //or include your 404 html file 
die; 
0

檢查用戶是否在數據庫中,如果沒有,你可以包含一個錯誤頁面。

$con = mysqli_connect("example.com", "user", "abc123", "db_name"); 
$result = mysqli_query($con,"SELECT * FROM user WHERE username = ".$_GET['username']); 
if($result == false) 
{ 
    require('error.php'); 
    exit(); 
} 

// Contine displaying user page. 

確保檢查SQL注入!