1

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

Hello here is my code:

<?php
        // zmienna $go przekazana metoda GET
      $go = $_GET['go'];

      // sprawdzamy czy zmienna $go nie jest pusta
      if(!empty($go)) {
        // sprawdzamy czy plik o danej nazwie istnieje
        if(is_file("page/$go.php")) include "page/$go.php";
        // jezeli plik nie istnieje wyswietla się komunikat
        else echo "<br />Nie ma takiej strony.";
      }
      // jezeli zmienna $go jest pusta wyswietla się strona glowna
      else include "page/8.php";

    ?>

And I am getting error:

Notice: Undefined index: go in C:\xampp\htdocs\sz\index.php on line 32

The line number 32 is:

$go = $_GET['go'];

Any help? It's annoying.

Community
  • 1
  • 1
trinny
  • 222
  • 1
  • 5
  • 22

3 Answers3

5
if( isset( $_GET['go'] ) ){
 $go = $_GET['go'];
}else{
 $go = 8;
}
Green Black
  • 5,037
  • 1
  • 17
  • 29
1

John has posted a useful solution and good method to set up your code; but also I think after that error is fixed you may get another error by searching the wrong directory caused by:

if(is_file("page/$go.php")) include "page/$go.php";

A correction would be:

if(is_file("page/" . $go . ".php")) include "page/" . $go . ".php"; // would go to page/32.php

If you do not separate your variable from your string it would be trying to seek a wrong link.

Devon Bernard
  • 2,250
  • 5
  • 19
  • 32
0

You need to check isset($_GET['go']) before attempting to access it. The code below addresses this issue.

<?php
if( isset($_GET['go']) && !empty($go) ) {
    $go = $_GET['go'];
    if(is_file("page/$go.php")) include "page/$go.php";
    else echo "<br />Nie ma takiej strony.";
}
else include "page/8.php";
Sammitch
  • 30,782
  • 7
  • 50
  • 77