如何从javascript中获取主页的url包括?

问题描述:

I have a page with a header include, and the include needs a phone number to change on it ONLY if the filename of the current page contains a certain word.

Normally, for a change that is not within an include, the below code would work.

<?php if (strpos($_SERVER['REQUEST_URI'],'example-match') !== false) 
{
    echo '555-555-5555';
} 
else 
{
    echo '1-800-555-5555';
}
?>

However, since the code is in an included file on the page, rather than the original page itself, the server instead is returning the name of that included file.

Example: www.example.com/im-on-this-page.html returns the name of the include the php file is in - www.example.com/header-include.php.

Is there a way to grab the URL of the page the include is "included on", rather than the url of the include, from code that is within the include?

UPDATE:

The file is being included via Javascript, since the page is it being included on is an html file:

<script>
$('#Header').load('/includes/header.php');
</script>

我有一个带有标题包含的页面,并且只有文件名时包含才需要更改电话号码 当前页面包含某个单词。 p>

通常,对于不在包含内的更改,以下代码可以正常工作。 p>

 &lt;?php if(strpos($ _ SERVER ['REQUEST_URI'],'example-match')!== false)
 {
 echo'555-555-5555'; 
} 
else 
  {
 echo'1-800-555-5555'; 
} 
?&gt; 
  code>  pre> 
 
 

但是,由于代码位于包含的文件中 该页面而不是原始页面本身,而不是返回该包含文件的名称。 p>

示例:www.example.com/im-on-this-page.html 返回php文件所在包含的名称 - www.example.com/header-include.php.

nn

有没有办法获取页面的URL include是“包含在” strong>,而不是include的url,来自include中的代码? p>

UPDATE: p>

Ť 他的文件是通过Javascript包含的,因为它包含的页面是一个html文件: p>

 &lt; script&gt; 
 $('#Header')。  ('/includes/header.php');
</script>
div>

As of your last comment:

Then it is not an include but a GET request to the PHP delivering a JavaScript. And then, the given URL is proper. In order to find out from where the script has been included you may either look for the referer header (which is instable) or pass the current file to the script itself:

<script> $('#Header')
 .load('/includes/header.php?from=encodeUriComponent("<?php echo $_SERVER['REQUEST_URI'] ?php>")'); </script> 

Within your script, instead of $_SERVER['REQUEST_URI'] then use $_GET['from']

As commentors have mentioned as well. This is incorrect. $_SERVER does not change for the duration of the script (and includes). Including a file will not alter it.

A javascript include, meaning it is actually called by the client by an ajax call, and not included on the server before the page is presented. (you load a page, and then load another page which you "print" somewhere on the first)

a quick and dirty fix would be to write something along these lines on the first php file

<?php if (strpos($_SERVER['REQUEST_URI'],'example-match') !== false) 
{
    echo "<script> var phone_number = '555-555-5555';</script>";
} 
else 
{
    echo "<script> var phone_number = '1-800-555-5555';</script>";
}
?>

which will create a variable on the client side, which you then can call from the loaded page as a javascript variable