PHP包含基于URL参数

时间:2014-10-02 13:36:32

标签: php php-include

我有一个包含30个php的目录,其中包含各种html块。我试图根据网址的参数包含5-6个这些文件。这些文件的名称如下:

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

网址如下所示:

www.example.com?include=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';
?>

感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分: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";
}

?>