phphttprequestcanonical-linkhttp-host

Print file name of including file, not included


I have example1.php and example2.php that include example3.php. In example3.php I have:

<link rel="canonical" href="http://website.com/<?php echo basename(__FILE__); ?>">

What I wanted is to have example1.php/example2.php in href dependent on the file including example3.php. The problem is that of course __FILE__ outputs example3.php. Is it possible to get the filename of the including file, not the included one?

Please don't suggesting using the XSS vulnerable SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].


Solution

  • (Warning: This trick may not scale, but will work in this exact case.)

    Use element 0 of the return from get_included_files(). That will be the original file that started the chain

    inside example3.php:

    $gif = get_included_files();

    then you can do

    <link rel="canonical" href="http://website.com/<?php echo basename($gif[0]); ?>">