简体   繁体   English

为什么我会收到未定义的索引错误?

[英]Why am I getting an Undefined Index Error?

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

$page = $p.".php";

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

?>

This block of code is giving me an Undefined Index error for the $page variable, i tried doing this...... 这段代码给了$ page变量的Undefined Index错误,我试过这个......

<?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");

?>

but this did nothing. 但这没有做任何事。 I'm still a novice at PHP and I'm not sure what I need to do to fix this. 我仍然是PHP的新手,我不知道我需要做些什么来解决这个问题。 Any suggestions? 有什么建议么?

EDIT: For everyone wanting the url: http://memphislinuxboy.tk/ ALso, for those of you wanting to know about security, the included file must be .PHP 编辑:对于每个想要网址的人: http ://memphislinuxboy.tk/另外,对于那些想要了解安全性的人来说,包含的文件必须是.PHP

I'd imagine $_GET['page']; 我想象$_GET['page']; isn't set, check the URL going in to make sure it contains "page=" at some point. 未设置,检查进入的URL以确保它在某些时候包含“page =”。

You can fix this with 你可以解决这个问题

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

Off topic: 无关:

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

You're attempting to include a file specified by the client. 您正在尝试包含客户端指定的文件。 This could be a serious security flaw if not handled correctly. 如果处理不当,这可能是一个严重的安全漏洞。 It's probably best to check against a list of known (good) files. 最好根据已知(好)文件列表进行检查。

the line $p = $_GET['page']; $p = $_GET['page']; is throwing the error. 抛出错误。

You're attempting to access an array element that has not yet been set. 您正在尝试访问尚未设置的数组元素。

Check to make sure the value exists before using it. 在使用之前检查以确保该值存在。

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

$_GET['page'] is not set in your code your url needs to contain ?page=somepage 你的网址需要包含的代码中没有设置$_GET['page'] ?page=somepage

<?php  

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

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

?>

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

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