2014-10-02 44 views
0

我有一個目錄,其中包含30個PHP包含各種HTML塊。我試圖根據url的參數包含這些文件的5-6個。該文件被命名爲這樣的:PHP包含基於URL參數

1.PHP 2.PHP 3.php ... 30.php

和URL看起來像這樣:

www.example.com ?包括= 1-4-14-20-29

我需要這樣的東西,但我知道這是無效的:

$include = @$_GET['include']; // hyphen separated list of file names. 

$arr = explode("-", $include); //explode into an array 
foreach ($arr as $filename) { 
include $filename."php"; 

}

結果我想獲得:

<php 
include '1.php'; 
include '4.php'; 
include '14.php'; 
include '20.php'; 
include '29.php'; 
?> 

感謝您可以提供任何幫助。

+2

不工作?爲了安全和安全,您應該列出輸入的白名單,例如使用情況'1','2',...和相應的開關。包括'1.php','2.php',..爲了不允許惡意包含請求 – fast 2014-10-02 13:40:55

回答

0

我會遍歷數組,所以纔會取最小值和最大值,像這樣:

<?php 
$include = $_GET['include']; // hyphen separated list of file names. 

$arr = explode("-", $include); //explode into an array 

# What's the min and max of the array? 
$max = max($arr); 
$min = min($arr); 

# Just to see what's happening. 
echo $max."-".$min; 

# Start at the lowest value, and work towards the highest in the array. 
for ($i = $min; $i <= $max; $i++) { 

# Include the nth php file 
include $i.".php"; 
} 

?> 

編輯

噢,對不起,剛纔看到我誤解你的問題,你不是在尋找範圍,而是在尋找個人電話。那麼這一個是你的:

<?php 
$include = $_GET['include']; // hyphen separated list of file names. 

$arr = explode("-", $include); //explode into an array 
foreach ($arr as $filename) { 
include $filename.".php"; 
} 

?> 
+1

非常感謝!那就是訣竅。 – emailcooke 2014-10-02 14:00:09