dtn43447 2015-11-09 18:15
浏览 17
已采纳

如何从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>
  • 写回答

3条回答 默认 最新

  • dongyun3805 2015-11-09 18:42
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100