0

Yesterday I was wondering about the possibility to use, in PHP, a file's name as argument for the file itself... Basically, I've a file named "file.php", and whenever, through the browser, I request any file i.e. "test1.php", I call the file "file.php" passing as argument the name of the previous file. Is possible to do something like this?

<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" (which isn't even in the server) calls "anotherpath/file.php", and this one can access the "test1" string. Thanks in advance!

2
  • 1
    I'd setup a query ?q=any that takes you to file.php and add a rewrite rule in .htaccess. I wouldn't keep the .php extension tho. Commented Jul 20, 2013 at 8:09
  • 1
    Doesn't this become available under $_SERVER['REQUEST_URI']? Commented Jul 20, 2013 at 8:35

3 Answers 3

1

Create a .htaccess file in the root directory and add this code to it.

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

Now, this will redirect all requests to index.php. Now, in index.php add this code,

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

This will print the requested URL into web browser.

Sign up to request clarification or add additional context in comments.

Comments

0

Instead on using rules, another option is to store the last page in the session. Something like;

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

Comments

0

send the file name in 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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.