1

I have sth like this:

<?php
    $body = $_GET["body"];
    if ($body=="")
    {
        include("includes/desktop.php");
    }
    else
    {
        if (is_file("includes/$body.php"))
        {
            include("includes/$body.php");
        }   
        else
        {
            include("includes/desktop.php");
        }        
    }                              
?>

How to make that this notice will disappear? It happens only when $_GET["body"] is empty.

Notice: Undefined index: body in C:\wamp\www\admin\index.php on line 106

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – George Mar 27 '14 at 10:45

4 Answers4

1

change

$body = $_GET["body"];

To

$body = isset($_GET["body"]) ? $_GET["body"] : '';

You can find almost all symbols and operators in php here

Community
  • 1
  • 1
Nauphal
  • 6,194
  • 4
  • 27
  • 43
0

It is warning you that body is not sent via $_GET. You need to do something like this

<?php
    if (isset($_GET["body"])) {
      $body = $_GET["body"];
      if ($body=="")
      {
          include("includes/desktop.php");
      }
      else
      {
          if (is_file("includes/$body.php"))
          {
              include("includes/$body.php");
          }   
          else
          {
              include("includes/desktop.php");
          }        
      }                              
  }
?>
dkasipovic
  • 5,930
  • 1
  • 19
  • 25
0

It's happening because there is no index body in the superglobal $_GET array. In other words, you're not passing $_GET['body'].

Instead use:

if (isset($_GET['body']) && $_GET['body']) {

That checks for both the index and a meaningful (non-falsy) value.

Mitya
  • 33,629
  • 9
  • 60
  • 107
0

Try

if (!isset($_GET['body']) || empty($_GET['body'])) {
   include("includes/desktop.php");
} else {
    ...
}

http://www.php.net/isset

On a side note: You should never trust user input. Directly passing the body GET parameter to your include() call allows a malicious user to include files you never intended to load.

bspellmeyer
  • 783
  • 5
  • 10