Get current page name with PHP and jQuery AJAX

时间:2022-07-03 23:44:20

I want to log the referer and landing page for visitors to a site. Here's the basic set up:

我想记录网站访问者的引用和登录页面。这是基本设置:

In my index.html file I include my javascript file: <script src="js/log.js"></script>. From here I use an AJAX call to a PHP file to get the referer and landing page like so:

在我的index.html文件中,我包含了我的javascript文件:

$.ajax({
    type: "post",
    url: 'php/functions.php',
    data: 'request_type=get_page_info',
    success: function (data) {
                console.log('DATA: '+data);
    }
});

In my PHP functions.php file I return the referer and landing page like so:

在我的PHP functions.php文件中,我返回引用和登录页面,如下所示:

if( isset($_POST['request_type']) &&  $_POST['request_type'] == 'get_page_info'){
   echo 'The referer is: '.$_SERVER['HTTP_REFERER'].' and 
         the landing page is: '.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}

All fairly straightforward. Say I came to this site from *.com I would expect the data in the AJAX success function to look like:

一切都相当简单。说我从*.com来到这个网站我希望AJAX成功函数中的数据看起来像:

The referer is: www.*.com and the landing page is: www.mysite.com/index.html

参考者是:www.*.com,登陆页面是:www.mysite.com/index.html

However, instead I am getting this:

但是,相反,我得到了这个:

The referer is: www.mysite.com/index.html and the landing page is: www.mysite.com/php/functions.php

参考者是:www.mysite.com/index.html,登陆页面是:www.mysite.com/php/functions.php

Where am I going wrong with this?

我哪里错了?

Thanks in advance.

提前致谢。

UPDATE

The reason I am trying this approach is because this small example is part of a larger project which is intended to be a plugin of sorts. To make it easy for users to integrate into their sites I want them to able to just include the javascript file in their page headers and that's it. So the option of changing index.html to index.php and just adding the php code there isn't really the approach I am looking for.

我尝试这种方法的原因是因为这个小例子是一个更大的项目的一部分,该项目旨在成为各种插件。为了方便用户集成到他们的网站,我希望他们能够将javascript文件包含在他们的页面标题中,就是这样。因此,将index.html更改为index.php并仅添加php代码的选项实际上并不是我正在寻找的方法。

3 个解决方案

#1


0  

You are echoing the referer of the ajax call itself, not your landing page. Since HTTP is stateless, you cannot get these information by an Ajax call without storing them somewhere, so you will need to put the following code in www.mysite.com/index.html and get rid of the ajax call:

你正在回应ajax调用本身的引用者,而不是你的登陆页面。由于HTTP是无状态的,因此无法通过Ajax调用获取这些信息而不将其存储在某处,因此您需要将以下代码放在www.mysite.com/index.html中并删除ajax调用:

<?php
if( isset($_POST['request_type']) &&  $_POST['request_type'] == 'get_page_info'){
   echo 'The referer is: '.$_SERVER['HTTP_REFERER'].' and the landing page is: '.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
?>

#2


0  

You can access the current page referrer in javascript:

您可以在javascript中访问当前页面引荐来源:

document.referrer

This is incase you are not using php for your main pages. If you are using php, you can capture it on page load.

这是因为你没有使用PHP作为主页。如果您使用的是php,则可以在页面加载时捕获它。

#3


0  

Referrer, when supported, would normally be the page that invoked the page in question. In your case, your functions.php is invoked from index.php (via AJAX), therefore you get your referrer as index.php and own page as functions.php. From the point of view of the server, it's not one page but two separate pages that are being requested - it doesn't know anything about AJAX or any other method of requesting pages.

在支持时,Referrer通常是调用相关页面的页面。在您的情况下,您的functions.php是从index.php(通过AJAX)调用的,因此您将referrer作为index.php并将自己的页面作为functions.php。从服务器的角度来看,它不是一个页面而是两个被请求的单独页面 - 它对AJAX或任何其他请求页面的方法一无所知。

What you want to do doesn't require any ajax at all - all you need is to put your code for getting the referrer into your main index.php file. If (for whatever strange reasons) you need this info in javascript, you can generate the javascript from your php file:

你想做的事情根本不需要任何ajax - 你需要的只是将你的代码放到你的主index.php文件中。如果(出于任何奇怪的原因)您需要在javascript中使用此信息,您可以从您的php文件生成javascript:

if( isset($_POST['request_type']) &&  $_POST['request_type'] == 'get_page_info'){
    echo "<script type='text/javascript'>\n;
    echo "    var referer = '" . $_SERVER['HTTP_REFERER'] . ";\n";
    echo "    var landing = '" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . ";\n";
    echo "</script>\n";
}

#1


0  

You are echoing the referer of the ajax call itself, not your landing page. Since HTTP is stateless, you cannot get these information by an Ajax call without storing them somewhere, so you will need to put the following code in www.mysite.com/index.html and get rid of the ajax call:

你正在回应ajax调用本身的引用者,而不是你的登陆页面。由于HTTP是无状态的,因此无法通过Ajax调用获取这些信息而不将其存储在某处,因此您需要将以下代码放在www.mysite.com/index.html中并删除ajax调用:

<?php
if( isset($_POST['request_type']) &&  $_POST['request_type'] == 'get_page_info'){
   echo 'The referer is: '.$_SERVER['HTTP_REFERER'].' and the landing page is: '.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
?>

#2


0  

You can access the current page referrer in javascript:

您可以在javascript中访问当前页面引荐来源:

document.referrer

This is incase you are not using php for your main pages. If you are using php, you can capture it on page load.

这是因为你没有使用PHP作为主页。如果您使用的是php,则可以在页面加载时捕获它。

#3


0  

Referrer, when supported, would normally be the page that invoked the page in question. In your case, your functions.php is invoked from index.php (via AJAX), therefore you get your referrer as index.php and own page as functions.php. From the point of view of the server, it's not one page but two separate pages that are being requested - it doesn't know anything about AJAX or any other method of requesting pages.

在支持时,Referrer通常是调用相关页面的页面。在您的情况下,您的functions.php是从index.php(通过AJAX)调用的,因此您将referrer作为index.php并将自己的页面作为functions.php。从服务器的角度来看,它不是一个页面而是两个被请求的单独页面 - 它对AJAX或任何其他请求页面的方法一无所知。

What you want to do doesn't require any ajax at all - all you need is to put your code for getting the referrer into your main index.php file. If (for whatever strange reasons) you need this info in javascript, you can generate the javascript from your php file:

你想做的事情根本不需要任何ajax - 你需要的只是将你的代码放到你的主index.php文件中。如果(出于任何奇怪的原因)您需要在javascript中使用此信息,您可以从您的php文件生成javascript:

if( isset($_POST['request_type']) &&  $_POST['request_type'] == 'get_page_info'){
    echo "<script type='text/javascript'>\n;
    echo "    var referer = '" . $_SERVER['HTTP_REFERER'] . ";\n";
    echo "    var landing = '" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . ";\n";
    echo "</script>\n";
}