PHP Front To Back
Lección 10 de 22 · PHP Front To Back [Part 10] – $_SERVER Superglobal · TI y software
PHP Front To Back [Part 10] – $_SERVER Superglobal
hey guys in this video we're going to start to look at superglobals so superglobals as you can see in the PHP documentation are built in variables
that are always available in all scopes okay so we can access them from a global scope from inside functions inside classes wherever they're basically system variables and there's all
different kinds we're definitely not going to get to all of them in one video for instance post will use when we work with processing forms get will use when we work with query strings inside of
URLs in this video I want to start with server ok and server is just that it holds information about not only the server but file paths client info like the clients browser that that's viewing
the web page the client IP address in a lot of cases so what I want to do is kind of do a little project where we create a web page that tells us information about the server and then
and also a little bit of information about the client ok so instead of just you know writing mindless code I want to actually create something so let's go ahead and open up our PHP sandbox I'm
going to create a new folder called website two and inside here we're going to create a file and save it as index dot PHP this is where we'll have our markup and then I just want another file
that we'll save as server will say server – info dot PHP ok and what I want to do is create an associative array for the server intro and also one for the client info and then we'll display it in
a nice web page and we'll even use Twitter bootstrap ok so let's go ahead and go into server info and create our PHP tags I'm actually going to create at the top well know we're going to need
the bottom as well ok and then this is going to be on the server server superglobal okay now notice with the superglobals they all begin with folks up for Global's they begin with
money sign underscore and then whatever that variable is alright so what we're going to do is create an array they create server array and then we're also going to create any client array okay so
it's just going to be an array with the different values and the superglobal so let's call this server and when I set it to just a set of brackets for an array and it's going to be an associative
array meaning that we're going to define the key and the value okay now if we click over here on server should tell us the different values we can use all right so let's take for instance server
name this gives us the name of the server host under which the current script is executing if the script is on a virtual host this will be the value defined for that virtual host so
basically it's the host server names so let's go ahead and add a key here which will be a string and we'll say host server name alright and we're going to set that to the server superglobal which
is an array and then we want server underscore name okay so that should give us the server name and then just to test it out let's go ahead and just echo server which is the name of our array
and then we want the host server name okay so let's save that and then we'll go to localhost / PHP sandbox /c website – slash server info dot PHP and it gives us a localhost
okay that's what we have for our host server name alright so next one I want to do is see let's do HTTP host which is the contents of host header from current request if
there is one so I believe in this case it's also going to be localhost but let's say host header and set that to server host on the school header ok and then we can change this to host header
let's see what that gives us undefined index host header I'm sorry not host header HTTP underscore host okay so that's also localhost alright so let's look at see I'm not going to do
all these some of these won't even work in my case let's look at server software where is that right here server identification string given in the header is when responding to requests so
this will basically tell us what the server is running so let's go ahead and add server software and set that to oops I don't like that server underscore software and let go that out okay save
that and tells us reusing Apache 2.4 point 25 windows 32 and we're using open SSL and PHP 7.1.1 ok so that's what that does just tells you what kind of server you're running OS things like that next
thing we want to know and this is something that you'll probably use a lot is your document root ok so the root of your web server so let's set that to server document underscore root ok and
what we can do here is instead of you know putting out each individual one we can actually do a print our and let's just look at the whole server array that we're creating created ok so now you can
see that document root is see slash xampp slash htdocs and if you are using xampp most likely it'll be the same thing all right so let's the next one I want to do is the current page and we
can get that you can get that with PHP self so PHP underscore self and a lot of times when you're submitting forms if you want to submit to the same page you're on you can put this as as the
action alright so let's go ahead and reload and now you'll see current page is in PHP sandbox web site – and then server info dot PHP alright another one we have is the script name and some of
these may be the same in in some cases we'll say server strip name we'll see that that is actually the same thing as well okay gives us the location of the
current script now if you want the absolute path because right here we just were going from htdocs but if you want the absolute path you can do that as well okay absolute path and set that to
server and this is going to be script underscore file name okay let's reload that and you can see we get from C Drive all the way to our current script all right so that gives
us all the server information not all of it I mean there are some other things here you can add you can go ahead and experiment but I don't want to put every single one in now for the client array
go ahead and comment that out we're going to set client equal to some brackets and first of all I'm going to do is we want to get the system info but the client using so it's the client
system info and let's set that to our server and we can get this with HTTP underscore user underscore agent all right and then let's do a print our for our client Garre okay so client system
info now this different manufacturer manufacturers have different strings for this I'm not exactly sure why it says Mozilla and Crowe in here but if you look at Crowe it says 50 version 56 dot
0 dot dot – 92 4.87 and if I go to what say help and about you'll see that that is in fact what I'm using okay it also says windows 64-bit which is what I'm on i'm
not sure why safari in mozilla are in here i don't use this very much but you can use it as a fairly unreliable way to tell what browser your clients are using all right so in addition to that we can
do the client IP address it will take a client IP and let's get to server underscore I'm sorry server and then remote underscore addr all right so let's save that and that's going to just
give me this colon colon one I'm actually on the same machine as my server so that's not going to work but generally this will return the users IP address okay you can also get the
remote port so let's say remote support and set that to server and then remote port okay and you can see that remote port from for this for me is six to seven three four alright so what I want
to do now is comment this out and we're going to go to our index.php page and build out a web page and include some of this info so let's put in our basic HTML tags for the titles and system info and
then I'm going to include bootstrap so I'm going to go to bootstrap dot bootstraps cbn.com and just grab this link here which is the CSS file all right so say a truss and paste that
in okay and so we shouldn't need anything else in the head so now in the body well actually we do need to bring our server info file and so appeal to a PHP include which we've already gone
older and we're going to include server – info dot PHP okay and then down here let's add a div and give it a class of container which is a bootstrap class just pushes everything in the middle
okay and then let's put an h1 anal space server the server and file info and under that what we're going to do is is test for the server to make sure it's there this variable now I haven't gone
over this yet but there's actually a shorthand peach shorthand PHP if syntax or not just yes but for loops or each all that stuff so what we can do is we can say PHP if server instead of you
know opening it here closing the tag and then doing it down for the closing bracket what we can do is just use a colon here and go like that and then down here say PHP and if okay so it's a
this is a better way to do it if you're actually inside of an HTML if you're inside HTML markup okay so if there's a server then let's put a UL here and we'll give it a class of list group all
right and then what we want to do is loop through each value in the server array and one output not only the value but also the key okay so let's do for each
so for each and we're going to we're going to use the key in the value so we'll say for each server value as and then the key and then also the value and we can also use that shorthand syntax
here as well so down here we can say PHP and for each okay and then for each value we have we want an Li I'm going to give this a class of whist group item and inside here let's see let's do the
key and the value I'm going to put the key inside of strong tags off okay so let's go and say PHP echo T let's echo key and then we put a colon in a space and then after the strong tag let's do
PHP echo value and that should do it so let's save that and then we're going to go to local host PHP sandbox slash website to can get rid of that server info file and there we go server and
file info we have the server name the host header the software the document root the current page that we're on the script name that we're on and then the absolute path of the page that we're on
okay so pretty handy now we want to do the same thing with the client stuff so let's just copy from this h1 to the end if and we'll just paste that in and let's say client info and then we'll
test for the client variable and then we just want to loop through the array that's in the client variable and everything else should stay the same so we'll save that reload and there's our
client info alright so that's it that's the server superglobal and we were able to create a nice little script there that tells us the information on the server as well as what we can on the
client okay so hopefully you guys like this our next few videos are going to be on different superglobals for instance form processing I'll show you how does how to pass along queries and URL things
like that all right so thanks for watching and I will see you in the next video
In this video we will create a simple webpage that displays the server, file and client info. We will use Twitter Bootstrap to make the page look nice and clean.
CODE: Code for this video
http://www.traversymedia.com/downloads/phpsandbox/phpsandbox8.zip
EDUONIX COURSES: Pleas use affiliate links from website below
http://www.traversymedia.com/eduonix-courses
SUPPORT: We spend massive amounts of time creating these free videos, please donate to show your support:
http://www.patreon.com/traversymedia
http://www.paypal.me/traversymedia
FOLLOW TRAVERSY MEDIA:
http://www.facebook.com/traversymedia
Posts by traversymedia
http://www.linkedin.com/bradtraversy