使用PHP filename作为文件本身的参数

时间:2013-07-20 08:06:50

标签: php .htaccess

昨天我想知道在PHP中使用文件名作为文件本身的参数的可能性......基本上,我有一个名为“file.php”的文件,无论什么时候,通过浏览器,我请求任何文件,即“test1.php”,我调用文件“file.php”作为参数传递上一个文件的名称。有可能做这样的事吗?

<Files *.php>
    //I call the "anotherpath/file.php" file passing as argument the filename of the file called before
    //i.e. Call anotherpath/file.php?arg=filename of the "virtual" file
</Files>

“test1.php”(甚至在服务器中都没有)调用“anotherpath / file.php”,这个可以访问“test1”字符串。提前谢谢!

3 个答案:

答案 0 :(得分:1)

在根目录中创建一个.htaccess文件,并将此代码添加到其中。

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

现在,这会将所有请求重定向到index.php。现在,在index.php中添加此代码,

<?php
    echo $_SERVER['REQUEST_URI'] ;
?>

这会将请求的网址打印到网页浏览器中。

答案 1 :(得分:0)

而不是使用规则,另一种选择是将最后一页存储在会话中。喜欢的东西;

$lastpage = $_SESSION['LastPage'];
$_SESSION['LastPage'] = 'this page.php';

答案 2 :(得分:0)

在url中发送文件名:

<a href="<?php echo $_SERVER['PHP_SELF'].'?p=page1';?>" >page1</a>
<a href="<?php echo $_SERVER['PHP_SELF'].'?p=page2';?>" >page2</a>
<div id="main-content" >
  <?php
    if(isset($_GET['p']))
      $page = $_GET['p'].".php";
    else
      $page = "default.php"
    include($page);
</div>