2011-05-09 53 views
0
if (!$_GET['page'] || preg_match('/\W/', $_GET['page']) || !file_exists('./intl/tpl/tpl_source/' . $_GET['page'] . '.tpl')) 
    $_GET['page'] = 'index'; 
    if ($_GET['page'] && $_GET['page'] != 'index') { 
    $smarty->assign("pg_" . $_GET['page'], true); 
    $smarty->display($_GET['page'] . ".tpl"); 
    die(); 
} 

此代碼讓我打開任何頁面(?頁= 1?頁= 2,依此類推,也很平均,如果沒有頁面給予,開放指數)

但我需要指定一個用戶可以打開,所以,代碼應該是這樣的:

if ($_GET['page'] = '21' || preg_match('/\W/', $_GET['page']) || file_exists('./intl/tpl/tpl_source/' . $_GET['page'] . '.tpl')) { 
//my stuff 
} 

總之,我需要指定地址的用戶可以用$ _GET [「頁」(頁= 21打開? ?page = 22等等)。 對不起,如果問題不清楚。

回答

0

您可以通過使用類型轉換簡化代碼和更簡單的允許頁面列表:

$allowed_pages = array(1, 12, 21, 25, 32); 

$page = (int)$_GET["page"] 
     and in_array($page, $allowed_pages) 
     and file_exists("./intl/tpl/tpl_source/$page.tpl") 
or $page = "index"; 

$smarty->assign("pg_$page", true); 
$smarty->display("$page.tpl"); 
die(); 
0

您可以創建一個白名單:

var $pages = array(
    21 => true, 
    22 => true 
); 
// or 
var $pages = array_flip(array(21, 22)); 

並測試該頁面是否存在:(!過濾)

if(isset($pages[$_GET['page']])) { 

}