0

我只是在我的本地主机服务器上拼凑了一个网站的开始——Apache、MYSQL、MYPHPADMIN。在我尝试使用 $_GET 动态链接导航之前,一切似乎都运行良好。这是 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>

当我检查我的链接以查看是否能够链接到我的各个页面时——主页、服务、关于我们等。它给了我这个错误:

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

所以我运行它来看看 $_GET 发生了什么:

var_dump($_GET);
exit;

数组为空。它什么也没显示。我以前使用过这种方法,但不同之处在于我在托管站点上。我检查了我是否有权限问题,但我检查了 apache.conf 并没有出现任何问题。

4

3 回答 3

1

先检查变量是否设置,检查是否有相关值:

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

不过要小心目录遍历攻击;因为您的代码会让攻击者查看任意文件。

于 2013-02-23T01:33:51.497 回答
1

你忘记了“。” 在文件名中(就在扩展名之前)。在代码部分之后:

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

添加以下内容:

$pg = $pg . ".";

或者,更改以下行:

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

至:

include ('content/'.$pg.'.php');
于 2013-02-23T01:34:20.700 回答
0

兄弟,我想你来自 TheDigiCraft。猜猜我也被困在那里并在 10 分钟前问了这个问题。在这里(https://stackoverflow.com/questions/17501271/i-have-error-in-coding-plz-fix-it-for-me)。只需用它替换 if else 代码就可以了:)

<?php

     echo"the page is now " ;

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

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

?>

顺便说一句,我的代码是这样的,它没有错误。我也在学习那门课程: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>
于 2013-07-06T09:08:32.130 回答