简体   繁体   English

PHP动态页面包括

[英]PHP Dynamic page include

I have an simple PHP script that includes a file, when it's requested via URL. 当通过URL请求时,我有一个包含文件的简单PHP脚本。

for an example: http://example.com/index.php?page=about So this includes the page about.inc 例如: http : //example.com/index.php? page=about因此,这包括about.inc页面

This is working, but i have another problem. 这正在工作,但是我还有另一个问题。 When someone visits the homepage so just: http://example.com/ . 当有人访问首页时,只需: 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. 我知道我可以禁用PHP中的错误通知,但这不是解决方案,而是一种解决方法。

$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? 我的问题是如何解决?

Try this: replace 3 no line by this: 试试这个:用3代替3行:

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

It will set empty in the $page variable if no page is provided. 如果没有页面,它将在$page变量中设置为空。 You have double $ in $path. 您在$ 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');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM