何时发送HTTP状态码? [英] When to send HTTP status code?

查看:119
本文介绍了何时发送HTTP状态码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前在我的PHP脚本中,当他/她尝试访问不存在或不属于该用户的内容时,我将用户重定向到自定义的404找不到错误页面。像这样:

  header('Location:http://www.mydomain.com/error/notfound/'); 
出口;

我意识到上面的header()调用默认发送302重定向状态码。



然而,我不明白的是,我应该发送404未找到的状态码。在我重定向用户之前?或者当我显示 / error / notfound / 页面时?



感谢您的帮助!

  header(HTTP / 1.0 404 Not Found); 
包含'error.php';
die();


Currently in my PHP scripts, I redirect the user to a custom 404 not found error page when he/she tries to access content that doesn't exist or doesn't belong to that user. Like so:

header('Location: http://www.mydomain.com/error/notfound/');
exit;

I realize the above header() call sends a 302 redirect status code by default.

What I don't understand, however, is when I should send the 404 not found status code. Before I redirect the user? Or when I display the /error/notfound/ page?

Thanks for your help!

解决方案

You should do something like this:

header("HTTP/1.0 404 Not Found");
include 'error.php';
die();

这篇关于何时发送HTTP状态码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆