Pergunta

Is there any way of retrieving the page name of the calling page for a php file?

For example, if I have index.php:

<?php
  session_start();
  include 'val.php';
  // other stuff
?>

And my val.php looks like:

<?php
  // get file name of caller (index.php in this case)
?>

I can use echo basename($_SERVER['PHP_SELF']); to get the page name if I put the val.php code in index.php, but I would like this code to appear on multiple pages, so extracting it to its own page is necessary.

Maybe I should put basename($_SERVER['PHP_SELF']); into a session variable in $_SESSION before calling include 'val.php', then destroy the variable after use? Is that a good idea?

Foi útil?

Solução

If I understand your issue correctly, you can use $_SERVER['SCRIPT_FILENAME'];

That will return the filename which included the val.php in this case.

Outras dicas

pathinfo() returns information about path: either an associative array or a string, depending on options.

(PHP 4 >= 4.0.3, PHP 5, PHP 7) pathinfo — Returns information about a file path

From here - http://php.net/manual/en/function.pathinfo.php

<?php
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');

echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0
?>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top