相互理解PHP 404重定向与无效的GET请求 [英] Understandin PHP 404 redirection related to invalid get request

查看:239
本文介绍了相互理解PHP 404重定向与无效的GET请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,现在用传统的PHP,没有框架,没有什么,我现在用简单的程序办法,现在我的问题是我在寻找了一段时间,但我没有得到回答我的问题,我没有使用.htaccess文件作为现在,但我真的需要了解404错误的原理?我有一个网站,在那里我会告诉这篇文章的相关类别,比如类= PHP的,所以我通过这个作为一个GET请求

Ok, am using traditional php, no frameworks, nothing, I am using simple procedural way, now my question is I was searching for a while but am not getting an answer to my question, I am not using .htaccess files as of now, but I really need to understand how 404 error works? I am having a website, where I show post's related to category, say category=php, so I pass this as a get request

$_GET['category'] == 'php';

现在目前有什么我做的是这样的:

Now currently what am doing is something like this :

$pocategory = $_GET['category'];

if($pocategory == 'php' || $pocategory == 'javascript') {
//Then show related posts
} else {
header('Location:404.php');
exit;
}

我的意思是我只是想PHP和JavaScript为有效的请求的值,其余的我想重定向到404,但我不理解怎么做,所以我也这样,如果我有50多个类别?我不能,如果条件一一列举在此,Inshort如何检测给定的GET请求值是否是无效的或不..

I mean I just want php and javascript as valid request's value, rest I want to redirect to 404 but am not understanding how to do it so I did this way, what if am having more than 50 categories? I cant list them all in this if condition, Inshort how to detect whether the given get request value is invalid or not..

任何帮助将大大AP preciated。

Any help will be much appreciated.

推荐答案

的.htaccess就是这样做的方式。

.htaccess is the way to do this.

ErrorDocument 404 index.php?404

这行会告诉Apache什么文件加载。上面的例子调用的index.php脚本。

that line will tell apache what file to load. The example above calls the main index.php script.

添加像这样到你的index.php文件的顶部:

add something like this to the top of your index.php file:

$error_404 = isset($_GET["404"]) ? true : false;

现在,你可以检测,如果你有一个404错误请求。 $ error_404将是真实的,那么为什么不添加一个简单的功能:

now you can detect if you have a 404 error request. $error_404 will be true, so why not add a simple function:

function error_404($error_404)
{
    if($error_404 == true)
    {
       // do some error stuff here, like set headers, and some text to tell your visitor
    }
}

现在只是调用你的函数:

now just call your function:

error_404($error_404);

最好做immidiatley的get处理后:

best to do that immidiatley after the get handler:

error_404($error_404)
$error_404 = isset($_GET["404"]) ? true : false;

或合并两成一条线:

or combine the two into one line:

error_404($error_404 = isset($_GET["404"]) ? true : false);


要解决的问题,将其添加到相关的脚本:


to address the question, add this to the relevant script:

$pocategorys_ar = array("php","javascript"); 

if (!in_array($pocategory, $pocategorys_ar)) 
{ 
    error_404(true); 
} 

请确保它可以访问error_404()函数。

Make sure it has access to the error_404() function.

这篇关于相互理解PHP 404重定向与无效的GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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