我用自己的模板系统制作了一个PHP框架,它在前端按照我想要的方式工作。模板系统使用 URL 的?url=($page)
部分来确定用户请求的页面,然后显示名为 ($page)
的文件内容。然后我使用 htaccess 来"漂亮"URL。
但是,我现在正在扩展框架以确定$_GET['url']
是否包含backend
,如果是,请查看它是否具有带有另一个字符串的尾部斜杠。例如,如果 $_GET['url']
的值为 backend/manage-gallery
,我希望它从后端文件夹中返回页面manage-gallery
。这是我现在拥有的代码。目前,如果 $_GET['url']
的值是后端,我让它静态检索页面(索引.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 值是否包含后端,并查看它是否有一个带有字符串的斜杠?这是我的访问
RewriteEngine On
RewriteRule ^(|/)$ index.php?url=$1
RewriteRule ^([a-zA-Z0-9_-]+)(|/)$ index.php?url=$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
。