2014-10-01 62 views
0

我想,所以我使用的服務器URI變量知道要保護的實際路徑網頁什麼的導航欄,用戶寫:PHP變量是否匹配目錄未設置可變

page.php文件

if ($_SERVER['REQUEST_URI'] == '/path/to/page.php') { 
    unset($_SERVER['REQUEST_URI']); // nothing will be displayed 
} else // page content 

它的工作正常,但現在的問題是?id = x或只是添加?將顯示錯誤頁面。

有沒有一種方法來添加或== ....

我想,以防止直接訪問,因爲我使用的是包括像這樣的index.php這些頁面路由器:網站。 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; 
+1

等待 - 需要更多信息。取消設置$ _SERVER ['REQUEST_URI']'將不會影響所顯示的客戶端瀏覽器的地址。你是否使用Apache mod_rewrite或類似的URL重寫?您是否使用'REQUEST_URI'來派生代碼中的鏈接路徑?請發佈更多相關的代碼,展示你如何真正使用它。 – 2014-10-01 20:31:21

+0

我已經在.htaccess中定義重定向到index.php我測試和它的工作,當我在導航欄中輸入路徑顯示什麼都沒有,但如果我添加?然後顯示頁面。 – 2014-10-01 20:45:48

回答

0

我不知道我的理解,但如果你這樣做方式:

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

這也適用於?我的意思是如果目錄只是.php或.php?...將取消設置變量?路徑/到/ page.php文件? - >未設置變量,「?」等等:?x – 2014-10-01 20:52:53

+0

我得到警告:preg_match():分隔符不能是字母數字或反斜槓 – 2014-10-01 20:57:05