Jump to content

Reading a $_SERVER Variable with an AJAX Call


iwato

Recommended Posts

BACKGROUND:  A visitor arrives on a website.  While still on the site he triggers an AJAX call that fills a <div> element with new HTML.  Once the page is filled another AJAX call is made that seeks to read the following value as encoded JSON:   $_SERVER['REMOTE_REFERER'].  Instead I receive a 500 internal server error.

The AJAX

(function() {
	$("#main").html('');
	$("<link/>", {
	   rel: "stylesheet",
	   type: "text/css",
	   href: "./_utilities/css/yourprofile_filler.css"
	}).appendTo("head");
	$.get('./yourprofile_filler.html', function(data) {
		$('#main').html(data);
	}).done(function(){
		$.ajax({
			url: './_utilities/php/visitor_ip.php',
			dataType: 'JSON',
			statusCode: {
				404: function() {
				alert( "Page not found" );
			}},
			success: function(visitor_ip) {
				console.log(visitor_ip);
			}			
		});
	});
})();

The PHP

<?php
	$referral_addr = $_SERVER['REMOTE_REFERER'];
	echo json_encode($referral_addr);
?>

ERROR MESSAGE

jquery.min.js:5 GET https://www.grammarcaptive.com/_utilities/php/visitor_ip.php 500 
(Internal Server Error)send 

Is the $_SERVER variable not available in the moment of the AJAX?  How do I otherwise make it available?

Roddy

Edited by iwato
Link to comment
Share on other sites

I'm not aware of a REMOTE_REFERER key in the  $_SERVER array. Perhaps you meant to use HTTP_REFERER. But even then, an undefined key would not throw a 500 error. I can't tell you why it's doing that from just the code you provided, you should check the server's error log.

If all you need is the referrer, though, Javascript already has that available without needing to send a request to the server. Use the document.referrer property.

  • Like 1
Link to comment
Share on other sites

Great.  Now, how does one go about converting the URL into an IP address? What I truly need is the IP address.  Even if I were to truncate everything after the domain name, how would I convert the domain name into a number like 64.102.73.1?

Does Javascript have a function that will deliver up the IP address directly?

Roddy

Link to comment
Share on other sites

No, you need to look up the DNS records for the domain name to find the A record which specifies the IP address.  It looks like WebRTC might include some way to do a local IP address discovery, but I'm not very familiar with that.  A much bigger question is why on earth you think you need Javascript to resolve a domain name to an IP address instead of just letting your computer do the work it needs to do.  What's the technical explanation for why you need that?

Link to comment
Share on other sites

JSG:  Although Matomo is a wonderful piece of software.  For a beginner such as myself it is a dense impenetrable thicket that I cannot easily manipulate to satisfy certain special needs that the developer's of the softward did not think to address. 

When a visitor download's a webpage from a given domain the Matomo server is notified and assigns a visitor ID based upon a collection of information received from the HTTP Request.  As I do not know how the Matomo visitor ID is created, I cannot accurately match the current user with the data stored about him in the Matomo database. 

There are two ways that I have discovered to access the data of a specific user:  the visitor ID assigned by Matomo and the visitor's IP address.

My objective is to know who is present so I know what to serve, and I have no idea how to obtain the visitor ID for a current visitor.  Although I am working on it.

Roddy

Link to comment
Share on other sites

It is OK.  I have since discovered what I hope to be is the proper Matomo get-method.  Although the following code works when I query my host server from my local server,  the same query fails when I upload the Javascript and PHP files to my host server and change the request URL to a directory path  between add-on domains of the same host server.  No matter what I try, I receive an Internal Server Error message. 

What follows is the code that both works and fails depending upon whether it resides on my local test server (suceeds) or on the page's online host server (fails).

The AJAX

$.ajax({
    url: './_utilities/php/visitor_ip.php',
    dataType: 'JSON',
    statusCode: {
        404: function() {
        alert( "Page not found" );
    }},
    success: function(visitor_ip) {
        $('#current_visitor_ip').html(visitor_ip.value);
    }			
});

The PHP

<?php
	$url = 'https://.../matomo/index.php?module=API&method=API.getIpFromHeader&format=JSON&token_auth=...';
	$curl_request = curl_init();
	curl_setopt($curl_request, CURLOPT_URL, $url);
	curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, true);
	echo (curl_exec($curl_request));
	curl_close($curl_request);
?>

I have since submitted a support ticket with my webhost server in an effort to clarify the problem.  I believe it has to do with the communication between add-on domains.  I have also been experimenting with an include statement and direct access to the PHP class that requests the method.  So far, no luck.

Roddy

 

Link to comment
Share on other sites

A 500 error will always have a log entry somewhere, assuming you don't have PHP configured to ignore error messages.  Between the PHP error log and the web server's error log, you should find the error message.

Link to comment
Share on other sites

This is the error log configuration, but the log is empty.

ini_set('log_errors', 1);
ini_set('error_log', './error.log');
error_reporting(E_ALL);

Roddy

Link to comment
Share on other sites

Make sure that the web server has write access for that file.  There will also be a message in the server's error log, Apache has its own error log, for example.  It will log the 500 response and any messages that go with it.

  • Thanks 1
Link to comment
Share on other sites

RAW HEADERS

Host: www.grammarcaptive.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:48.0) Gecko/20100101 Firefox/48.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
X-Requested-With: XMLHttpRequest
Referer: https://www.grammarcaptive.com/overview.html
Cookie: _pk_id.1.d6fd=b04f8c5a7bf9190d.1525557969.12.1526435670.1526435593.; _pk_ref.1.d6fd=%5B%22%22%2C%22%22%2C1526435593%2C%22http%3A%2F%2Flocalhost%2F~iwato%2Findex.html.en%22%5D; _pk_ses.1.d6fd=*
Connection: keep-alive

RESPONSE HEADERS

Connection: close
Content-Length: 556
Content-Type: text/html; charset=iso-8859-1
Date: Wed, 16 May 2018 01:54:58 GMT
Server: Apache

Might the problem lie in the fact that the request is over a secure connection and that cURL requires additional parameters?

Roddy

Link to comment
Share on other sites

Recently I created an experiment in order to obtain some insight into the source of the problem.  Please find below the experiment, and its results.

EXPERIMENT ONE (matomo_practice.html):

https://www.grammarcaptive.com/_utilities/php/matomo_practice.html

<p>'Can you see me?'</p>

Result: Can you see me?

________________

EXPERIMENT TWO (matomo_practice_2.html):

https://www.grammarcaptive.com/_utilities/php/matomo_practice_2.html

<?php
echo ('Can you see me?');
?>

Result: a blank page.

_________________

EXPERIMENT THREE (matomo_practice.php):

https://www.grammarcaptive.com/_utilities/php/matomo_practice.php

<?php
echo ('Can you see me?');
?>

Result: Internal Server Error.

_________________

I sent these three link with the above information to Lunarpages (my host server), and they replied that I should correct my problem of excess usage.  Well, 'my problem' of excess usage was addressed several weeks ago.  Where before I was witnessing usage faults on the order of several and sometimes many 10s in a day, today you can count the total number of faults on one or two hands on any given day.  As the above three links always behave in the same way, no matter when they are tried, it is highly unlikely that the problem is associated with excess usage.

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8" />
	<title>Matomo - PHP Direct</title>
	<meta name="generator" content="BBEdit 10.5" />
</head>
<body>

... The CONTENT OF THE THE THREE SEPARATE EXPERIEMENTS ...
  
</body>
</html>

FURTHER NOTE:  These three files are placed in a folder where the vast majority of PHP files on my website are stored.  This problem does not appear to exist with the other files in the same folder.

Roddy

 

Edited by iwato
Link to comment
Share on other sites

???

Depends on what type a page the content was added to

(1) html -> is what is expected

(2) html -> is what is expected but! it would show blank, but the php code would still show in view source.

(3) PHP -> General 500 error, for some reason that can't be identified.

Link to comment
Share on other sites

According to my host server,  the internal server error is related to excess usage.  This makes no sense, however, for no matter the time or day that I try to access these three files the result is always the same.  More recently I tried to discover the file permissions for the troubled file, only to receive an internal server error for the file that was used to perform the investigation.

Roddy

Link to comment
Share on other sites

Maybe you should check this, if i remember rightly, your site goes about collecting lots of information from pod-casts and like to display on your site, did seem slow in processing, if you are on shared server, it maybe combination of the others sharing the server and yours that put it over the edge.

https://www.hostpapa.co.uk/knowledgebase/causes-excessive-shared-web-hosting-server-usage/

If you check the Network tab of Web Developer tool it should give indication of what is taking excessive amount of usage time to process.

Edited by dsonesuk
Link to comment
Share on other sites

I have called my host server, and they said that they are working on the problem.  I am now of the belief that the problem lies elsewhere, and I, and likely others, are the victims of someone else's ignorance or neglect on my shared server on the one hand, and malicious intent on the part of 3rd-party others on the other.  Government corruption breeds asocial behavior, and we have no shortage of either today, as the former grows larger and larger.

Roddy

Link to comment
Share on other sites

Maybe they have you on a plan where you are limited by processor time used or something like that.  If you've already hit your processing cap then any request to a PHP page would show an error, because the server isn't going to run the code.  That would normally be a monthly cap or something that resets at the end of the month.  Your second experiment is not a blank page though, if you view the source code in the browser you'll see the contents of that file.  Your server just isn't set up to execute PHP inside an HTML file, so it just returns the code.

Link to comment
Share on other sites

The problem was resolved  at  approximately 5:00 PM PST.  I have been dead in the water for nearly 24 hours with no explanation as to how the problem was resolved.  Try the links!  They are now all in good working order.  I did nothing to cause the problem and  everything to try and resolve it.  In the end, however, I am sure that everything that I did only served as a stimulation to get others to resolve it.

In the meantime I developed a round-robin AJAX call to match visitor IP addresses with those stored in the Matomo database.  At least, it is a start in finding the perfect match.  I will return to this problem later.

As always, thank to everyone at W3Schools for your wonderful support!

Roddy

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...