Join Kevin Skoglund for an in-depth discussion in this video Display a full request report, part of Easy PHP Projects: Single-Serving Sites.
- In this movie, we're going to build on the work that we did in the previous chapter. We've learned how to display the user's IP address. Now we're going to work to report back all of the information we can see about the web request that's coming in. And it turns out that that's quite a bit of information. You can view a full list of the key value pairs that are being set in the server Superglobal at this URL on the php.net website. We're going to be looking at some of the most important ones. The first ones I want us to look at are probably the most important and most often used, and those are HTTP_USER_AGENT and HTTP_REFERER.
The USER_AGENT is the browser type. It's the information that the browser sends about itself with the request. It's a way that we can tell whether the user is using Internet Explorer, Firefox, Safari, or something else. REFERER tell us where the user came from before they got to us. Now it may be blank if they just simply typed the URL directly into their browser bar, but if they clicked on a link from another site, then it should send that page that they were on as the referer with this new request.
These two bits of information are probably the most frequently used information from web analytics packages. They analyze this information on every request that comes in and give you charts and graphs so that you can keep track of it so you can see who your users are and where they're coming from. I also want to call attention to the fact that REFERER is intentionally misspelled here. In the early days of the internet, they made a mistake and spelled it with only three Rs instead of the four that it's supposed to have, but the standard went into wide use before they caught the mistake.
And so it has stuck around in its misspelled format for all of these years. So that's not a typo. It is actually HTTP_REFERER. Now when we want to use these, of course, we're getting these out of the server Superglobal. So we tell the server Superglobal to return the value that can be found from the key HTTP_USER_AGENT and HTTP_REFERER. And that's the same thing that we did when we were working with REMOTE_ADDR earlier.
There are other useful values there as well. For example, we have REQUEST_TIME. That's going to be the time of the request. When we actually see it, you're going to see that it looks a bit odd because it's not telling us the time in a human readable format, it's a Unix timestamp, that is it's the number of seconds since January 1st of 1970. We'll talk a bit more about that in the next movie. The REQUEST_URI is the URL that's actually being requested. The REQUEST_METHOD is the method of the request.
If you're clicking on a link, it's probably a GET Request. If you're submitting a form, then it's most likely a POST Request. There's also a PUT Request and PATCH Request and HEAD Request, but GET and POST are the two most common ones that you'll see. The QUERY_STRING is going to be the query string, if any, which was used when this page was accessed. HTTP_ACCEPT is going to say which media types are considered acceptable for the response. ACCEPT_CHARSET, ACCEPT_ENCODING, and ACCEPT_LANGUAGE are going to do the same thing but for the character set, the encoding preference, and the language preference.
HTTPS will be a non-empty value when we're using the HTTPS protocol. That is a secure connection, an encrypted connection, and that's when you see the little lock up in your browser letting you know that you have a secure connection perhaps to your bank's website or something like that. This lets us know that it's a request made over a secure connection. And then REMOTE_PORT, the port that's being used on the user's machine in order to communicate with this web server. Now there are many more than this. You can refer to the php.net URL I showed you earlier if you want to see the full list.
For now, let's move over to PHP in order to create a single-serving site that reports back this information from each browser request. Once again, I'm going to be working in my sites directory. So I'm going to open up a new TextMate document, and I'm going to save this in that sites directory. Save, and I'm going to put it inside Sites, and I'm going to call it Browser_mirror.php. So there it is. You can see it right here along with the other files we were working with before.
And I'm actually going to open up that whatismyip_final that we were working with at the end of the last chapter. And I'm going to just select all and copy that. And the reason why is because I want that HTML and CSS out of there. So I'm going to take all of the inside bits here out, all of the PHP code and functions out, and I'm going to just fold up all this CSS. TextMate allows me to just fold those up out of the way. And instead of being What Is My IP? we're going to call it Browser Mirror, and I'll copy that and put it down here in the h1 as well.
All right, so that gives us a good formatted HTML document to start with. You can format yours however you like. I just wanted to use that as a starting place. Okay, so now we want to mirror back those values. So let's start with the first one as one we know. Let's work with that Remote IP address again. We just saw how to do that in the last chapter. The Remote IP of the request. We can use php to echo back the value from the SERVER Superglobal for a REMOTE_ADDR and then let's at the end put another p tag.
Okay, now we know also that there's more complicated things about proxies and forwarded IP addresses. I'm not going to deal with that here because we dealt with that in the last chapter. I'm going to keep it simple and just use REMOTE_ADDR. Now we want to make another one. I'm going to just copy that and paste it to save me a little time. This is going to be the User Agent. So we're going to report back to the User Agent, and the value for that is HTTP_USER_AGENT. You can look those up on those previous slides, if you need a reference.
Now instead of typing each and every one of these over and over, I'm going to sort of fast forward ahead, by just pasting in all of them. You can pause the movie if you want to copy them down. You can do the legwork of actually writing each one yourself, either by copying and pasting and making edits like I just did, and you can use the slides for reference to make sure you get them all. Make sure you didn't make any typos. Make sure that you spelled REFERER the correct way for internet usage. So once we've got all of those in there, now it should display for us.
Let's go over to Firefox. We'll open it up and, in order to load that webpage for me, I need to go to localhost/ then ~ kevinskoglund browser_mirror.php, and that will load the page. Now see it's still centered here. I'm just going to change that real quick. I'm going to make it so that the p tags are text-aligned: left and I'll also put margin: 0; padding: 0 on them.
All right, let's come back and let's load it again. All right, that's a little easier to read. So everything now is over here on the left side. So the Remote IP address, we see we got the same thing we got in the last chapter for that. The User Agent string, you'll see we got a very long complicated string. We'll come back to this and we'll talk a bit more about it, but it's telling us all the critical information we need to know about what browser is accessing the page right now. We don't have Referer because we didn't click a link to get here. We typed it directly into the browser bar.
If we'd clicked a link to get here, then we would have a Referer. The Request Time, again this is in Unix time, it's the Unix timestamp. It's the number of seconds since January 1st, 1970. We'll talk about that in the next movie. The URL that was requested to get here. It's the same thing that you see up here. Now while that may seem obvious, the key thing here is that this is what gives PHP the ability to know what page was being accessed. So PHP can parse this and act on it in different ways.
Then there's the GET Request Method because we typed it in directly here. If we'd submitted a form, it would probably be a POST request. The Query String, there was none. If we loaded the page again with page=1, you'll see that the Query String is page=1. And then HTTP Accept, Charset, Encoding, Language, those are a little deeper, and you don't necessarily need to know what all of those mean. They're settings that are being sent to the server to tell the server how it should reply back to the request.
So those can be helpful for debugging in some cases. HTTPS, you'll see it's just blank because we're not using a secure connection. And then Remote Port telling us what port the user is actually communicating on, and in my case, that's Port 49322. Now I'm going to just make a small improvement here to this HTTPS line because I kind of hate the fact that it's just blank. I'd really rather it said Yes or No. Are we using HTTPS or not? So the way I'm going to do that is I'm going to take away the echo here, and turn this into an if statement, if(isset and if that's set, we're getting some value back, then we'll know, we can echo 'Yes' else we can echo 'No', we are not using a secure connection.
And I don't need that second semicolon, just one after No. So let's save it and let's just reload the page real quick. I think that's a little nicer, just telling us right there that we're not using HTTPS. So there you go. We now have a page, as a single-serving site, that will tell us information about the request that's coming in. This might be useful for debugging or maybe you want to find out information. Maybe you want to have one of your users that you're trying to troubleshoot information from, they want to be able to go there, and then tell you what information they see.
So you can help them troubleshoot a problem or something. This just simply is a mirror showing you what the request looks like. In the next movie, I want us to talk a little more about this Request Time and see if we can clean that up a little bit.
- Finding and validating an IP address with PHP
- Parsing the HTTP request for browser details
- Comparing dates and times in PHP
- Randomizing data with PHP
- Styling page content dynamically
- Generating random sentences