使用PHP文件名作为文件本身的参数
昨天我想知道,在PHP中,是否可以使用文件名作为文件本身的参数。。。基本上,我有一个名为“file.php”的文件,每当我通过浏览器请求任何文件(如“test1.php”)时,我调用文件“file.php”,并将上一个文件的名称作为参数传递。有可能这样做吗使用PHP文件名作为文件本身的参数,php,.htaccess,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 befo
<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>
//我调用“anotherpath/file.php”文件作为参数传递前面调用的文件名
//i、 e.调用另一个路径/file.php?arg=虚拟文件的文件名
“test1.php”(甚至不在服务器中)调用“anotherpath/file.php”,这个可以访问“test1”字符串。提前谢谢 在使用规则时,另一个选项是存储会话中的最后一页。有点像
$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>
在根目录中创建一个.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浏览器中。我会设置一个查询?q=any
,它会将您带到file.php
,并在.htaccess
中添加一个重写规则。我不会保留.php
扩展名tho。这不是在$\u服务器['REQUEST\u URI']
下可用吗?