Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么会出现未定义的索引错误?_Php_Indexing_Undefined - Fatal编程技术网

Php 为什么会出现未定义的索引错误?

Php 为什么会出现未定义的索引错误?,php,indexing,undefined,Php,Indexing,Undefined,这段代码为$page变量提供了一个未定义的索引错误,我尝试了 这样做 <?php $p = $_GET['page']; $page = $p.".php"; if(file_exists($page)) include($page); elseif($p=="") include("home.php"); else include("404.php"); ?> 但这并没有起到任何作用。我仍然是PHP的新手,我不确定我需要做什么来解决这个问题。

这段代码为$page变量提供了一个未定义的索引错误,我尝试了 这样做

<?php  
$p = $_GET['page'];

$page = $p.".php";

if(file_exists($page))
    include($page);
elseif($p=="")
    include("home.php");
else
    include("404.php");

?>

但这并没有起到任何作用。我仍然是PHP的新手,我不确定我需要做什么来解决这个问题。 有什么建议吗

编辑:对于想要url的所有人:
另外,对于那些想了解安全性的人,包含的文件必须是.PHP

我想
$\u GET['page',请检查进入的URL,以确保它在某个时候包含“page=”

你可以用它来解决这个问题

<?php  
$p = $_GET['page'];

if(isset($page)){$page = $p.".php";}

if(file_exists($page))
    include($page);
elseif($p=="")
    include("home.php");
else
    include("404.php");

?>

离题:

if(isset($_GET['page']) {
    //your code
}

您正试图包含客户端指定的文件。如果处理不当,这可能是一个严重的安全漏洞。最好对照已知(良好)文件列表进行检查。

$p=$\u GET['page'正在抛出错误

您正在尝试访问尚未设置的数组元素

使用前请检查以确保该值存在

if(file_exists($page))
    include($page);

$\u GET['page']
未在您的代码中设置您的url需要包含的
?page=somepage

if (isset($_GET['page']))
{
    $page = $_GET['page'];
}
else
{
    // this is the page they'll have if they havnt specified one
    $page = 1;
}


你的url是否包含这样的内容….?page=xxx….如果没有,你没有得到任何名为“page”的get变量,这段代码让我害怕:
?page=/etc/php.ini
@CD001为什么因为你认为有人可以使用
$\u get
访问它?OP不能在
.haccess
中使用=>
命令允许、拒绝所有
来保护它吗?@Fred阅读您所写的内容,并考虑一下PHP的include()函数是如何工作的。@Fred,他可以,但最好使用模式或预定列表来检查哪些文件是允许的。如果apache配置不正确,他也可以使用/etc/passwd。。。它需要说明!;)这种插入会在我的站点上导致404但是谢谢你的信息!哪一行?除非您的输入错误,否则此处不应导致404
<?php  

    $page = (isset($_GET['page']) ? $_GET['page'] : 'home') . '.php';

    if(file_exists($page)){
        include($page);
    } else {
        include("404.php");
    }

?>