0

我用自己的模板系统制作了一个 PHP 框架,它可以在前端按我的意愿工作。模板系统使用?url=($page)URL 的部分来确定用户请求的页面,然后显示名称为 的文件的内容($page)。然后我使用 htaccess 来“美化”这个 URL。

但是,我现在正在扩展框架以识别 if $_GET['url']contains backend,如果是,请查看它是否有一个带有另一个字符串的斜杠。例如,如果值为$_GET['url'],我希望它从后端文件夹backend/manage-gallery中返回页面。manage-gallery这是我现在拥有的代码。$_GET['url']目前,如果值为后端,我可以静态检索页面(index.php) 。

public function addTpl() {     
    global $zip;

    if(!isset($_GET['url']) || empty($_GET['url'])) {
        $_GET['url'] = 'index';
    }

    $front = '_zip/_templates/_front/'. $zip['Template']['Front'];
    $back  = '_zip/_templates/_back/'. $zip['Template']['Back'];

    if(strpos($_GET['url'], 'backend') !== false) {
       if(file_exists($back . '/')) {
            if(file_exists($back . '/index.php')) {
                ob_start();
                include($back . '/index.php');
                $this->tpl .= ob_get_contents();
                ob_end_clean();
            } else {
                if($zip['Template']['Front'] == 'Refresh/multi-page') {
                    ob_start();
                    include($back . '/404.php');
                    $this->tpl .= ob_get_contents();
                    ob_end_clean();
                } else {
                    die(zipError('File Not Found', 'The file <b>' . secure($_GET['url']) . '</b> could not be found. Please re-check the URL; If you were directed here using a link, please report that link <b><a href="mailto:IntactDev@gmail.com">here<a/>.</b>'));
                }
            }
        } else {
            die(zipError('Template Not Found', 'The template <b>' . $zip['Template']['Front'] . '</b> could not be found. Please check your configuration file for a mistake.'));
        } 
    } else {
        if(file_exists($front. '/')) {
            if(file_exists($front. '/' . secure($_GET['url']) . '.php')) {
                ob_start();
                include($front. '/' . secure($_GET['url']) . '.php');
                $this->tpl .= ob_get_contents();
                ob_end_clean();
            } else {
                if($zip['Template']['Front'] == 'Refresh/multi-page') {
                    ob_start();
                    include($front. '/404.php');
                    $this->tpl .= ob_get_contents();
                    ob_end_clean();
                } else {
                    die(zipError('File Not Found', 'The file <b>' . secure($_GET['url']) . '</b> could not be found. Please re-check the URL; If you were directed here using a link, please report that link <b><a href="mailto:IntactDev@gmail.com">here<a/>.</b>'));
                }
            }
        } else {
            die(zipError('Template Not Found', 'The template <b>' . $zip['Template']['Front'] . '</b> could not be found. Please check your configuration file for a mistake.'));
        }
    }
}

如何让我的代码检查 get 值是否包含后端,并查看它是否有一个带有字符串的斜杠?这是我的 htaccess

RewriteEngine On
RewriteRule ^(|/)$ index.php?url=$1
RewriteRule ^([a-zA-Z0-9_-]+)(|/)$ index.php?url=$1
4

1 回答 1

1

由于斜杠总是在backend字符串之后,我会strpos通过添加斜杠来更改:

if(strpos($_GET['url'], 'backend/') !== false) {

另外,如果backend出现在字符串中,它总是在字符串的开头,所以我会在零位置寻找它:

if(strpos($_GET['url'], 'backend/') == 0) {

然后,我将使用&&并向该if语句添加另一个条件来检查字符串是否超过 7 个字符。换句话说,如果backend/在开头,并且字符串长于 7 个字符,则 . 之后的字符会更多backend/

if(strpos($_GET['url'], 'backend/') == 0 && strlen($_GET['url']) > 7) {

.htaccess编辑:你的文件有问题。更改$1$2

于 2014-11-24T22:45:52.627 回答