‫All right.
‫So when working with web applications, it's essential to have access to various server related information.
‫And in PHP this information is made available through the the server Superglobal array.
‫And there'll be times when you need to access things like the file path, server name, the user agent
‫string, and other values.
‫And these variables provide a convenient way to do so.
‫So we're going to go over some of the common server variables.
‫I'm not going to go over all of them because some of them you'll just never use.
‫Even some of these you'll probably never use.
‫But these are the most common.
‫So basically what we have here is just some HTML.
‫And I have a div.
‫So a bunch of divs here that have the title of whatever it is that we're going to get from the superglobal.
‫And then I'm outputting a variable.
‫So here you can see request method server protocol etcetera.
‫But these are all set to just empty strings for now.
‫And I'm going to just go over each one and show you what they are and how to use them.
‫All right.
‫So the first one is request method.
‫And and by the way all this this is available in the starter code for in the PHP sandbox for this lesson.
‫So request method.
‫This contains the Http request methods such as get, post or put.
‫It helps determine the type of request being made, which is very important because you're going to
‫want to know if that script is running from a Post request, if someone submitted a form or a Get request,
‫if someone just went to the URL, or put if someone's updating content, delete if they're deleting
‫content.
‫So the way that we can get this, I'm just going to get rid of this string here.
‫And we're going to use the server Superglobal which is underscore server.
‫And then in brackets here because it's an associative array we're going to say request underscore method
‫in all caps.
‫So if I go ahead and reload that you'll see that this is from a Get request.
‫Right.
‫Because I'm just going to the URL.
‫So that's a Get request.
‫If I were to submit a form to this file.
‫In fact I can do that real quick.
‫I didn't plan on it.
‫But let's just go down here and.
‫I'm going to get rid of this after.
‫But if I say method on my form and I set that to post right, and I don't put an action, that means
‫it'll submit to this particular file, and then we'll have an input with a type of submit.
‫Okay.
‫And if I do that, and then I reload.
‫Let's see.
‫Why is the form right here?
‫So if I press submit now you'll see that this changes to post because I made a post request to this
‫script.
‫All right.
‫So that's request method.
‫Now one thing I should should mention is when you're going to use these.
‫And if I reload it's going to reload as the last request which was post.
‫So if I go up here in the URL and I hit enter, then it's going to go back to get.
‫But one thing I want to mention is that when you use these, you should always check for them first.
‫So you can do that in a couple of ways.
‫You can use is set.
‫So I could have an if statement and say if is set server request method.
‫Or I could use the null coalescing operator which is much easier.
‫So I'm just going to use that and then just make this variable here an empty string if this doesn't
‫exist.
‫So we'll do that for all of these.
‫All right.
‫So the next one is server protocol.
‫So we can get that.
‫You know what I'm going to do is just.
‫Um, yeah, I'm just going to copy.
‫Because I'm going to be doing this quite a bit, so I'm just going to copy this.
‫So that I can just paste that in.
‫So this one is going to be server underscore protocol.
‫So these are all underscore format.
‫All right.
‫And what this does is it stores the name and version of the protocol of the request like Http 1.1 etcetera.
‫So let's see what that gives us.
‫And yours might be different depending on how your server is set up.
‫But you can see mine is Http 1.1.
‫So the next one is server name.
‫Whoops.
‫Yeah.
‫Let's just replace that with server underscore name.
‫And if I reload you'll see that we get localhost.
‫And yours will probably be localhost as well unless you have it set up differently.
‫But this just holds the name of the server host or I should say the name or our domain.
‫If you were if this was if we deployed this to a server and a domain name, then it would be whatever.com.
‫So next we have the server port okay.
‫So that's going to be server underscore port.
‫And if I reload, I'm going to get 8000 because I'm running my dev server on 8000.
‫Yours might be something different.
‫So the next one is server software.
‫So let's say server underscore software.
‫Okay.
‫So let's see what I get.
‫So I'm running the PHP 8 to 7 development server.
‫Usually it's going to be like Apache or Nginx or something like that.
‫But I'm using the dev server so that's what I get.
‫Next one is the server admin.
‫So let's see what that gives us.
‫Server underscore admin.
‫And I get nothing for that.
‫And but what this is, is a variable that stores the email address of the server administrator or webmaster.
‫I don't have that set up, so I don't have anything for it.
‫The document root.
‫This is something that you'll probably use quite a bit.
‫So let's see what that gives us first.
‫So you can see that my document route is in my users folder, my name and then projects and then PHP
‫sandbox.
‫So that's where the script that I'm currently running is.
‫That's the route of the document.
‫Yours obviously will be something different if you're using Larrigan for instance, it'll be your C
‫drive Larrigan W-w-w.
‫If you're using Xampp, it'll be C drive slash Xampp really depends on on how you have things set up.
‫So next we have the script file name.
‫So it's a script underscore file name.
‫I reload, it just gives me the name of the script, which is index.php and the full location.
‫Okay.
‫So we also have script name.
‫So let's say script underscore name.
‫And that's going to be just the script name not the file path.
‫So it's just index.php.
‫Okay.
‫Next we have PHP self.
‫So let's say PHP underscore self.
‫Reload.
‫That is also going to give me index.php, and you could even use this in your form if you're submitting
‫to the same script.
‫Of course you don't.
‫If you leave off the action, it'll serve to that script.
‫But you could also use this if you wanted.
‫So remote address this will store the IP address of the client making the request.
‫Okay, so let's say remote not remove remote underscore ADR.
‫Reload that.
‫And obviously if you're on your local host, it's going to be your local host, which is 127001.
‫That's the loopback address.
‫Then we have the connection.
‫So let's say if we go server and it's going to be Http underscore connection.
‫And that's just going to give us keep alive.
‫So this this indicates the type of connection being used, such as keep alive or close.
‫Okay.
‫We also have the host.
‫So that will be Http underscore host.
‫And that should be for me localhost 8000.
‫So that's the host and the port that I'm running it on.
‫We also have referrer.
‫So this is Http underscore.
‫Refer.
‫I think that's how you spell.
‫All right.
‫Um.
‫Did I say that right?
‫Yeah.
‫So it stores the URL of the referring page if available.
‫I don't have that a referring page.
‫I didn't click on a link to come here.
‫I just went to the URL bar.
‫But if you're in production and someone clicks on a link on another website to take you to that page,
‫then that that link or that URL will be in this this variable.
‫Next we have the user agent and this is what holds the user agent string, which typically identifies
‫the client's browser and operating system.
‫So this can be really useful to know what what users are using to to come to your your project your
‫application.
‫So it's Http underscore user agent.
‫And if I reload, you can see mine, says Mozilla.
‫Now I'm on Chrome.
‫This has this is a little confusing how it says Mozilla, and this just comes from back in the day for
‫compatibility issues.
‫Even if you're on Safari or Chrome, it'll still say Mozilla in here.
‫But you can see I am on a mac.
‫I'm using OSX, so yours might be different depending on your operating system and whatever browser
‫you're using.
‫And there's other ways to get it in an easier way and a cleaner and a cleaner way.
‫There's different packages that you can install that will say, you know, Chrome or Firefox or Safari.
‫ET cetera.
‫So next one is query string.
‫So let's say query underscore string.
‫So that will give me the query string.
‫And you can see I don't have any query string right now.
‫But if I were to go up to my URL bar and put a question mark and let's say name equals Brad, and then
‫I come to this page, you'll see under query string I have name equals Brad.
‫Okay.
‫So we could also get this through the get get superglobal which I'll get to in a little bit.
‫But this is a way that you can get the exact query string and not just the variable.
‫And then we have request Uri.
‫So this represents the full URL of the current request including the query string.
‫So let's do.
‫Uh.
‫Let's see.
‫It's going to be request.
‫Underscore Uri.
‫And it's if I don't have the query string, it's just going to be slash.
‫If I do have it, then it's going to show it.
‫If it's a different page, like I don't know, test dot PHP, then it would show test dot PHP and then
‫any query strings.
‫All right.
‫So you can see that this stuff is pretty useful for different reasons.
‫Whether you need a certain file path or you need to request method.
‫This one is really important that you'll use a lot if you're if you're using vanilla PHP.
‫When we create our router and our project, this this is going to be the main, the main component of
‫that router, because we need to know which kind of requests are being sent.
‫All right.
‫So that's the server Superglobal.
‫In the next one we're going to look at the env and the globals superglobal.