如何告诉服务器显示404页面

时间:2014-09-23 13:04:26

标签: php

如何在找不到用户时显示404页面 考虑从$ _GET变量中提取用户名的网站,如

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

如果在数据库中找不到用户,则应显示404页面。

提前致谢

2 个答案:

答案 0 :(得分:0)

示例:

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

答案 1 :(得分: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注入!