dimanche 27 avril 2014

Get current page name with PHP and jQuery AJAX


Vote count:

0




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: http://ift.tt/fyw30c and the landing page is: http://ift.tt/1jgkQDu



However, instead I am getting this:



The referer is: http://ift.tt/1jgkQDu and the landing page is: http://ift.tt/1hAV7al



Where am I going wrong with this?


Thanks in advance.



asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire