2011-05-09 87 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']])) { 

}