dougu5950 2014-01-01 11:54
浏览 32
已采纳

PHP动态页面包括

I have an simple PHP script that includes a file, when it's requested via URL.

for an example: http://example.com/index.php?page=about So this includes the page about.inc

This is working, but i have another problem. When someone visits the homepage so just: http://example.com/ . It will show me this

Undefined index: beta in /home/admin/domains/mydomainname.nl/public_html/index.php on line 3

That is:

$page = $_GET['page'];

I know I can disable the error notice in PHP, but this is not a solution but a workaround.

$path = 'app/inc/pages/dynamic/';
$page = $_GET['page'];
$php  = '.inc';
$both = $path . $page . $php;
$pages = array('index', 'home', 'about', 'contact');
if (!empty($page)) {

    if(in_array($page,$pages)) {
        //$page .= '.php';
        include($both);
    }
    else {
    include('app/inc/pages/dynamic/404.inc');
    }
}
else {
    include('app/inc/pages/dynamic/home.inc');
}
}

It does include the homepage via

else {
    include('app/inc/pages/dynamic/home.inc');
}

My question is how to solve this?

  • 写回答

1条回答 默认 最新

  • dowm41315 2014-01-01 11:57
    关注

    Try this: replace 3 no line by this:

      $page = isset($_REQUEST['page']) ? $_REQUEST['page'] : '';
    

    It will set empty in the $page variable if no page is provided. You have double $ in $path. Full code:

    $path = 'app/inc/pages/dynamic/';
    $page = isset($_REQUEST['page']) ? $_REQUEST['page'] : '' ;
    $php  = '.inc';
    $both = $path . $page . $php;
    $pages = array('index', 'home', 'about', 'contact');
    if (!empty($page)) {
    
        if(in_array($page,$pages)) {
            //$page .= '.php';
            include($both);
        }
        else {
            include('app/inc/pages/dynamic/404.inc');
        }
    }
    else {
        include('app/inc/pages/dynamic/home.inc');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比