代码信号PHP - 强制404? [英] Codeigniter & PHP - forcing a 404?

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

问题描述

在codeigniter,如你所知,一个形式的页面: / class / function / ID ,其中class是控制器名称,function是控制器中的方法,并且ID是要传递给该方法的参数。



典型的用法是(对于书站点)将书id传递给函数然后查询数据库中是否有相应的书。我的问题是这样:我是搞乱和随机(在url字符串)键入在数据库中不存在的ID(正常点和点击浏览这将永远不会发生),我得到数据库错误由于残留查询我尝试使用不存在的ID执行。



我写了代码来检查在尝试使用ID之前是否返回任何行,但如果ID是不存在我想让用户得到一个404错误页,而不是一个空白页或某事(因为这似乎是正常的功能)。这需要是一个真正的404页面(不是简单地加载视图,看起来像404页面),以便不与搜索引擎。好吧,所以我的问题是这样:在正常的程序逻辑流(如上所述)如何使用codeigniter强制404错误?感谢。



更新:代码igniter具有 show_404('page')但我不认为这会产生真正的HTTP 404错误...

解决方案

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



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



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

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

该行将HTTP状态设置为404.这是搜索引擎读取页面所需要的全部作为404页面。


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.

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.

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.

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

解决方案

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

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

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

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

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.

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

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