Вопрос

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'].

Это было полезно?

Решение

(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]); ?>">

Другие советы

IF you dont want to use any $_SERVER[''] types to get the file name You can use

pathinfo(__FILE__, PATHINFO_BASENAME)

to just get the filename you want

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top