Codeigniter &PHP - 强制 404? [英] Codeigniter & PHP - forcing a 404?

查看:33
本文介绍了Codeigniter &PHP - 强制 404?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在codeigniter中,如你所知,页面的形式为:/class/function/ID,其中class是控制器名称,function是控制器内的方法,ID是参数传递给那个方法.

In codeigniter, as you know, a page of the form: /class/function/ID, where class is the controller name, function is the method within the controller, and ID is the parameter to pass to that method.

典型的用法是(例如对于图书站点)将图书 ID 传递给函数,然后该函数将查询数据库以查找合适的图书.我的问题是这样的:我乱七八糟地(在 url 字符串中)输入了一个数据库中不存在的 ID(使用正常的点击浏览,这永远不会发生),并且由于剩余查询而出现数据库错误我尝试使用不存在的 ID 执行.

The typical usage would be (for a book site for example) to pass the book id to the function which would then query the database for appropriate book. My problem is this: I was messing around and randomly (in the url string) typed in an ID that is not present in the database (with normal point and click browsing this would never happen) and I get database errors due to the residual queries I attempt to perform using a non-existent ID.

在尝试使用 ID 之前,我已经编写了代码来检查是否有任何返回的行,但是如果 ID 不存在,我希望用户获得 404 错误页而不是空白页或其他内容(因为这似乎是正确的功能).这需要是一个真正的 404 页面(不仅仅是加载一个看起来像 404 页面的视图),以免与搜索引擎混淆.好的 - 所以我的问题是:在正常的程序逻辑流(如上所述)中,我如何使用 codeigniter 强制出现 404 错误?谢谢.

I have written code to check if there are any rows returned before attempting to use the ID, but if the ID is non-existent I would like the user to get a 404 error page rather than a blank page or something (since this seems like proper functionality). This would need to be a true 404 page (not simply loading a view that looks like a 404 page) so as not to screw with search engines. Okay - so my question is this: within normal program logic flow (as described above) how can I force a 404 error using codeigniter? Thanks.

更新:代码点火器有一个 show_404('page') 函数,但我认为这不会产生真正的 HTTP 404 错误......

Update: code igniter has a show_404('page') function but I don't think this will generate a true HTTP 404 error...

推荐答案

show_404() 实际上为搜索引擎发送适当的标头以将其注册为 404 页面(它发送 404 状态).

show_404() actually sends the proper headers for a search engine to register it as a 404 page (it sends 404 status).

使用 Firefox 插件检查调用 show_404() 时收到的标头.您将看到它发送正确的 HTTP 状态代码.

Use a Firefox addon to check the headers received when calling show_404(). You will see it sends the proper HTTP Status Code.

检查默认的application/errors/error_404.php.第一行是:

Check the default application/errors/error_404.php. The first line is:

<?php header("HTTP/1.1 404 Not Found"); ?>

该行将 HTTP 状态设置为 404.这就是搜索引擎将您的页面读取为 404 页面所需的全部内容.

That line sets the HTTP Status as 404. It's all you need for the search engine to read your page as a 404 page.

这篇关于Codeigniter &amp;PHP - 强制 404?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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