php在特定的地方执行包含

时间:2014-10-01 23:52:56

标签: php include

我使用动态包含制作了一个路由器,我试图" print" index.php(路由器所在的页面)中包含的include()。

的index.php

require_once('includes/functions.php');

sec_session_start();

$views                      = 'views/';
$site                       = $views . 'site/';
$user                       = $views . 'user/';
$admin                      = $views . 'admin/';

// whitelist
$includes = array(
    '/home'                 => $views . 'home.php',
    '/error'                => $views . 'error.php',
    '/login'                => $site . 'login.php',
    '/logout'               => $site . 'logout.php',
    '/contact'              => $site . 'contact.php',
    '/about'                => $site . 'about.php',
    '/register'             => $site . 'register.php',
    '/register_success'     => $site . 'register_success.php',
    '/profile'              => $user . 'profile.php',
    '/update'               => $user . 'update.php',
    '/admin'                => $admin . 'admin.php'
);

if ($_SERVER['REQUEST_URI'] == '/')
    $_SERVER['REQUEST_URI'] = '/home';

preg_match('/^([\w\/]+)/', $_SERVER['REQUEST_URI'], $matches);
$matches[1] = isset($matches[1]) ? $matches[1] : null;

include('themes/default/views/layouts/main.php');

main.php

if(array_key_exists($matches[1], $includes)) {
    $content = include($includes[$matches[1]]);
} else $content = include('views/error.php');

修正了,第一次应该给出一些错误,但删除缓存(ccleaner)就可以了。

链接如:site.com/home和site.com/login?error=1(在带/ home的html中,所以它不会给出任何错误)

谢谢你!

0 个答案:

没有答案