【问题标题】:php if variable match dir unset variablephp 如果变量匹配 dir 未设置变量
【发布时间】: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


【解决方案1】:

我不知道我是否理解,但如果你这样做:

if (preg_match('\/path\/to\/page\.php', $_SERVER['REQUEST_URI'] ))

【讨论】:

  • 这也适用吗?我的意思是如果目录只是 .php 或 .php?...会取消设置变量吗?路径/到/page.php? -> 未设置变量,“?”等等:?x
  • 我收到警告:preg_match():分隔符不能是字母数字或反斜杠
猜你喜欢
  • 1970-01-01
  • 2012-04-11
  • 2019-07-25
  • 2018-05-08
  • 2014-01-15
  • 2013-08-27
  • 1970-01-01
  • 2013-01-01
  • 1970-01-01
相关资源
最近更新 更多