繁体   English   中英

在PHP中验证变量的怪异问题

[英]Weird issue with verifying variable in PHP

我有这段代码应该检查页面是否不存在,或者是否包含不应该包含的任何内容,并且由于某种原因,如果我转到除以下页面之外的其他页面,则会向我抛出错误,尤其是406主页($ _GET =“”)。

这是代码,在此先感谢您的帮助:)

$currentpage = $_GET['a'];
$pages[1] = "";
$pages[2] = "help";
$pages[3] = "work";
$pages[4] = "download";
$pages[5] = "process";
$pages[6] = "safariex";
$pages[7] = "services";

if(isset($_GET) && !ctype_alpha($_GET) && $_GET['a'] != ""){
    header("Location: http://pattersoncode.ca/error.php?ec=406");

}
if (!ctype_alpha($_GET['a']) && $_GET['a'] != "") {
    header("Location: http://pattersoncode.ca/error.php?ec=406");
}
if ( ! in_array( $currentpage, $pages ) )
{
 header("Location: http://pattersoncode.ca/error.php?ec=404");
}

我确实认为这是错误的:

!ctype_alpha($_GET)

$_GET是一个数组,而不是字符串。 ctype_alpha($_GET)将始终等于false。

您可能想要这样:

if(!isset($_GET["a"]) || !ctype_alpha($_GET["a"])) {
    header("Location: http://pattersoncode.ca/error.php?ec=406");
    exit();
}

这应该同时照顾到406个条件。

在大多数情况下,您还需要在执行重定向时执行exit()。

如果可能的话,最好发送一个实际的http响应代码,而不是重定向:

header('HTTP/1.1 406 Not Acceptable', true, 406);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM