简体   繁体   中英

Error message whenever I open the index.php page

I'm working on a website and this is what I have for my index.php:

 <?php

    $p     = $_GET['p'];
    $pages = array('home', 'culture', 'design', 'art', 'about');
    $path  = 'http://localhost:8080/projects';

    include('header.php');


    if(!isset($p) || !in_array($p, $pages)) {
        include('header.index.php');
        include('content.index.php');
    } else {
        switch($p) {
            case "home";
                include('header.home.php');
                include('content.home.php');
            break;
            case "culture";
                include('content.culture.php');
            break;
            case "design";
                include('content.design.php');
            break;
            case "about";
                include('content.about.php');
            break;
            case "art";
                include('content.art.php');
            break;
            default:
                include('content.index.php');
            break;
        }
    }
    include('footer.php');
    ?>

I get the following error:

**Notice: Undefined index: p in C:\wamp\www\projects\index.php on line 3
Call Stack
#   Time    Memory  Function    Location
1   0.0523  680200  {main}( )   ..\index.php:0**

When you assign p initially, p is not set in $_GET So you can do this

 $p     = isset($_GET['p']) ? $_GET['p'] : null;

If you don't care about notices, You can disable them in your php.ini by changing error_reporting to E_ALL & ~E_NOTICE, however I wouldn't recommend it

Just a suggestion maybe try !empty()

if(!empty($p) || !in_array($p, $pages)) {
    include 'header.index.php';
    include 'content.index.php';
}

The switch statement you have is somewhat bogus, especially as you already have the $page array. You actually want to verify if the page exists or load the index page (probably?):

$p     = isset($_GET['p']) ? (string) $_GET['p'] : NULL;
$pages = array('home', 'culture', 'design', 'art', 'about');
$path  = 'http://localhost:8080/projects';
if (!in_array($p, $pages)) {
    $p = 'index';
}
// include $p based on $path

However, you still have the problem with the header. So this is the lesson: make the header part of every include. You can stack as many includes as you like, just take care that every include contains it's correct header. Then you're done. And you won't see any warnings.


So the code after following what @hakre suggested should look like this:

$p     = isset($_GET['p']) ? (string) $_GET['p'] : NULL;
$pages = array('home', 'culture', 'design', 'art', 'about');
$path  = 'http://localhost:8080/projects';

include('header.php');
if (!in_array($p, $pages)) {
    $p = 'index';
    include('header.index.php');
    include('content.index.php');
}

Thanks @hakre for your help..

This is NOT an error. As the log claims this is a NOTICE. It is meant to inform you about a potential problem, but does not keep the script from being executed.

In this case the interpreter tells you that the array $_GET does not contain an element with index 'p'. It is not initialized, probably cause it has not been specified in the request in this case.

Try to test first if the element exists before you try to access it. Use isset() for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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