【发布时间】:2014-10-01 20:28:48
【问题描述】:
我想保护页面不受实际路径影响,因此我使用服务器 uri 变量来了解用户在导航栏中写的内容:
page.php
if ($_SERVER['REQUEST_URI'] == '/path/to/page.php') {
unset($_SERVER['REQUEST_URI']); // nothing will be displayed
} else // page content
它工作正常,但现在问题是 ?id=x 或只是添加 ?将显示有错误的页面。
有没有办法添加 OR == ?....
我想阻止直接访问,因为我使用的路由器在 index.php 中包含这些页面,如下所示:site.com/page 和 site.com/page?...
谢谢!
编辑:添加更多信息:
.htaccess
Options -Indexes
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
index.php 中的路由器
// array whitelist for match
$includes = array(
'/home' => 'dir/to/home.php',
'/other' => 'dir/to/other/page.php'
);
if ($_SERVER['REQUEST_URI'] == '/')
$_SERVER['REQUEST_URI'] = '/home';
preg_match('/^([\w\/]+)/', $_SERVER['REQUEST_URI'], $matches);
$matches[1] = isset($matches[1]) ? $matches[1] : null;
if(array_key_exists($matches[1], $includes)) {
$content = include($includes[$matches[1]]);
} else $content = include('views/error.php');
return $content;
【问题讨论】:
-
等待 - 需要更多信息。取消设置
$_SERVER['REQUEST_URI']不会影响显示的客户端浏览器地址。您是否通过 Apache mod_rewrite 或类似方法使用 URL 重写?您是否使用REQUEST_URI在您的代码中派生链接路径?请发布更多相关代码,展示您实际使用它的方式。 -
我已经在 .htaccess 中定义重定向到 index.php 我测试它并且它正在工作,当我在导航栏中输入路径时没有显示任何内容,但如果我添加?然后显示页面。
标签: php global-variables unset