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:
$.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:
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 stackoverflow.com I would expect the data in the AJAX success function to look like:
The referer is: www.stackoverflow.com and the landing page is: 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
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.
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:
<?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'];
}
?>
You can access the current page referrer in 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.
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.
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:
if( isset($_POST['request_type']) && $_POST['request_type'] == 'get_page_info'){
echo "<script type='text/javascript'>
;
echo " var referer = '" . $_SERVER['HTTP_REFERER'] . ";
";
echo " var landing = '" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . ";
";
echo "</script>
";
}