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


Using PHP filename as argument for the file itself

昨天我想知道在PHP中是否可以使用文件名作为文件本身的参数。。。基本上,我有一个名为"file.php"的文件,每当我通过浏览器请求任何文件(即"test1.php")时,我都会调用文件"filephp",并将上一个文件的名称作为参数传递。有可能做这样的事情吗?

<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"字符串。提前感谢!

在根目录中创建一个.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'] ;
?>

这将把请求的URL打印到web浏览器中。

另一个选项是存储会话中的最后一页,而不是使用规则。类似于;

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

发送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>