0

I was just throwing together the beginnings of a website on my localhost server -- Apache, MYSQL, MYPHPADMIN. All appeared to be working fine until I attempted to link up the navigation dynamically using $_GET. Here is the code in index.php:

<?php include('Config/setup.php') ?>
<?php

if ($_GET['page'] == ''){
$pg = 'home';
} else {
$pg =$_GET['page']; 
}

?>

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>FTS</title>

<link rel="stylesheet" type="text/css" href="css/Styles.css"/>

</head>

<body>
 <div class ="header temp_Block">
      <?php include('templates/header.php');?>
</div>

<div class ="main_nav temp_Block">
    <?php include('templates/main_nav.php');?>
</div>
<div id ="Content">
    <div class ="main_content temp_Block ">
        <?php
        include ('content/'.$pg.'php');

        ?>


    </div>
</div>     
    <div class = "footer temp_Block">
    <?php include('templates/footer.php');?>
    </div>

</body>

</html>

When I checked my links to see if I was able to link to my various pages -- home, services, about us etc. Its giving me this error:

Notice: Undefined index: page in C:\xampp\htdocs\test\index.php on line 6

Warning: include(content//content/homephp): failed to open stream:
No such file or directory in C:\xampp\htdocs\test\index.php on line 35

Warning: include(): Failed opening 'content//content/homephp' for inclusion
(include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\test\index.php on line 35

So I ran this to see what was going on with $_GET:

var_dump($_GET);
exit;

The array is empty. IT shows nothing. I've used this method before but the difference was I was on a hosted site. I checked to see if I had permissions problems but I checked apache.conf and nothing appeared to be wrong.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Cvrpapc
  • 199
  • 2
  • 3
  • 11
  • How does the accessed URL look in your browser address bar for above result? Also note the missing period here: `include ('content/'.$pg.'php');`. Has this ever worked? Take care of filtering (path traversal) with at least `basename()`! – mario Feb 23 '13 at 01:34
  • You should use [isset()](http://php.net/isset) instead of comparing against empty string. – Ja͢ck Feb 23 '13 at 01:35
  • Scope. Your $pg is a local var within the brackets. Unsure if that's a big deal with php, I'm new as well. Search variable scope and php manual comes up in google, has some examples like your site and when to use local var vs global access – Stephen J Feb 23 '13 at 01:35
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Michael Berkowski Feb 23 '13 at 01:35
  • 1
    @Stephen this is **not** about scope. Scope only matters in functions. – Ja͢ck Feb 23 '13 at 01:37
  • @MichaelBerkowski this is not a dup. The problem is that he's trying to include a file, but he uses the file-name `homephp` instead of `home.php` – Nir Alfasi Feb 23 '13 at 01:38
  • 1
    DONT include files via user input! this is a huge security risk – Oden Feb 23 '13 at 01:38
  • @user2089405 both Oden & Jack are right! pay attention! – Nir Alfasi Feb 23 '13 at 01:39

3 Answers3

1

Check to see if the variable is set first, and then check to see if it has a relevant value:

if (isset($_GET['page']) && $_GET['page'] !== ''){
    $page = $_GET['page'];
} else {
    $page = 'home';
}

Be careful about directory-traversal attacks, though; as it is your code will let attackers view arbitrary files.

Waleed Khan
  • 11,426
  • 6
  • 39
  • 70
1

You forgot the "." in the file-name (just before the extension). After the section of code:

if ($_GET['page'] == ''){
  $pg = 'home';
} else {
  $pg =$_GET['page']; 
}

add the following:

$pg = $pg . ".";

or alternatively, change the following line from:

include ('content/'.$pg.'php');

to:

include ('content/'.$pg.'.php');
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
0

Bro i think you came from TheDigiCraft . guess what i got also stuck there and asked this question like 10 mins ago. here (https://stackoverflow.com/questions/17501271/i-have-error-in-coding-plz-fix-it-for-me) . just replace if else code with this it will work:)

<?php

     echo"the page is now " ;

     if(isset($_GET['page']) == '')

         {
            echo "home";
         }
     else
         {
            echo $_GET['page'];
         }

?>

btw my piece of code is this and it has no errors . i am also learning that course :D .

<?php
// setup code here
include('config/setup.php');


?>


<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title> Dynamic Website Project </title>
<link rel="stylesheet" type="text/css" href="css/styles.css">

</head>
<body>

<div class="header temp_block">
    <?php include('template/header.php'); ?>
</div>

<div class="nav_menu temp_block">

<?php include('template/main_nav.php'); ?>

</div>

<div class="content temp_block">
    <?php

     echo"the page is now " ;

     if(isset($_GET['page']) == '')

         {
            echo "home";
         }
     else
         {
            echo $_GET['page'];
         }
    ?>
</div>

<div class="footer temp_block">

<?php include('template/footer.php'); ?>

</div>

</body>
</html>
Community
  • 1
  • 1
Junaid
  • 436
  • 1
  • 5
  • 14
  • 'if(isset($_GET['page']) == '')' will never pass. The isset command returns true or false. The results will never match a empty string. – CharlesEF Aug 19 '19 at 21:09
  • Sorry, should have added - You should use 'if(isset($_GET['page']) && $_GET['page'] == '')'. – CharlesEF Aug 19 '19 at 21:21