2011-10-23 31 views
0

此文件中的所有內容都以$ _GET ['q']啓用主域名和網站中其他目錄的方式設置: else if (substr($_GET['q'], 0, 7) == 'quotes/') 如果我想擁有文件夾,名爲:終極 - 交易 - 和低價格,我會用我是否需要使用substr()來調出一個目錄或者我可以使用另一個代碼?

else if (substr($_GET['q'], 0, 30) == 'ultimate-deals-and-low-prices/') 

這是否有意義,否則我會打電話給我以另一種方式的目錄,而不必讓PHP調用目錄的子串文件名。我知道這是什麼子字符串,但我只看到它用於少於10個字符。如果我的目錄有更多的字符,我仍然會使用子字符串?

這是if語句的本文檔中的開頭:

require_once(getcwd() ."/db/db_pagetitle.php"); 
print get_page_title($_GET['q']); 
if (isset($_GET['q']) && $_GET['q']) { 
    if (file_exists(getcwd() .'/pages/'. $_GET['q'] .'.php')) { 
     require_once(getcwd() .'/pages/'. $_GET['q'] .'.php'); 

比它進入

else if (substr($_GET['q'], 0, 7) == 'quotes/') 

我怎麼叫我的目錄?

謝謝?

+1

我三次讀到你的問題,我仍然不知道你想知道什麼。 – middus

+0

如果我給你'?q = ../passwords',假設getcwd()會發生什麼情況。 'passwords.php'存在。安全問題? – rodneyrehm

回答

0

您仍然可以使用SUBSTR,還有具有超過10個字符:)換句話說沒有問題,你可以使用完全相同的代碼,您已經發布(如果你不介意的打字):else if (substr($_GET['q'], 0, 30) == 'ultimate-deals-and-low-prices/')

但是,如果對於所有文件夾(例如,cd,或者在其中搜索文件)存在一些通用邏輯,則不必爲每個文件夾編寫其他分支,只需使用以下內容:

if (isset($_GET['q']) && $_GET['q']) { 
    if (file_exists(getcwd() .'/pages/'. $_GET['q'] .'.php')) { 
     require_once(getcwd() .'/pages/'. $_GET['q'] .'.php'); 
    } else if (is_dir(getcwd() . '/pages/' . $_GET['q'])) { 
     // do whatever you want to do with the folder 
    } 
} 

如果您要編寫更多代碼,將會更容易理解您正在嘗試執行的操作。

0

如果您打算使用多個關鍵字,則可以使用檢測系統。

$match[]='ultimate-deals-and-low-prices/'; 
$match[]='users/'; 
$match[]='imgs/'; 
foreach($match as $k=>$v){ 
$l=strlen($v); 
$s=substr($_GET[q],0,$l); 
if ($v==$s) //Code to run... 
} 
0

無論strpos發生什麼事情?

<?php 

if (file_exists($sanitizedPath)) { 

} elseif (strpos($_GET['q'], "ultimate-deals-and-low-prices/") === 0) { 

} elseif (strpos($_GET['q'], "some-other-directory/") === 0) { 

} 

減少類型,減少錯誤,更快。

相關問題