Locked lesson.
About this lesson
What are Superglobals, and how do you use them?
Exercise files
Download this lesson’s related exercise files.
Superglobals.docx59 KB Superglobals - Solution.docx
59.2 KB
Quick reference
Superglobals
Superglobals are special PHP variables that are available anytime.
When to use
Each Superglobal serves a specific purpose.
Instructions
Here's the master list of Superglobals:
$_SERVER
$_GET
$_POST
$_FILES
$_COOKIE
$_SESSION
$_REQUEST
$_ENV
Hints & tips
- $_SERVER
- $_GET
- $_POST
- $_COOKIE
- $_SESSION
- 00:05 In this video I wanna talk about superglobals, and superglobals are special
- 00:09 variables, and they're built into PHP and you can access them anytime from anywhere.
- 00:15 You don't have to define them.
- 00:16 You don't have to do anything.
- 00:17 They're just available to you.
- 00:18 And there's a few of them.
- 00:20 I'm just gonna come through here and
- 00:22 delete all this stuff that we did in the last video.
- 00:24 We're just gonna paste these in, and we can take a look at these.
- 00:29 These are the superglobals.
- 00:31 And we've already looked at GET and POST when we did our form page.
- 00:35 We used them right here.
- 00:37 And you can see this is generally how we access and how we use superglobals.
- 00:41 We name them, and then we put these brackets, and then inside of here,
- 00:44 that's what we're gonna do with that superglobal.
- 00:47 And you'll see what that means in just a minute.
- 00:49 So GET and POST we've already done.
- 00:54 FILES, that goes with POST.
- 00:56 This deals with uploading files via a form.
- 00:59 We're not gonna really look at that.
- 01:02 You can look that up if your interested.
- 01:03 But cookies in sessions we're gonna talk about the next couple of videos.
- 01:07 And request in environments they're not really all that useful, so
- 01:11 we're going to ignore those.
- 01:13 So, in the rest of this video we're gonna look at this server superglobal,
- 01:16 'cause it is actually super useful and kinda interesting, and so
- 01:21 let's go ahead and take a look at that.
- 01:23 Just gonna delete these.
- 01:27 If we head over to Google and type in PHP superglobals
- 01:31 we can pull up the PHP.net manual page.
- 01:35 Remember we've looked at these before.
- 01:37 And here's just a list of them.
- 01:38 You can click on each one and read a little more if you're interested.
- 01:41 But we can look at this server one.
- 01:44 And what this does is it gives you all kinds of information about the server that
- 01:47 you're running.
- 01:47 And we are running this Cloud 9 development environment.
- 01:52 It's running all the server and all the stuff for us.
- 01:55 It's got Apache running and all this good stuff.
- 01:58 But we can actually access information about that via our PHP file or program.
- 02:04 And there's lots of different times you're gonna wanna do that.
- 02:07 So what can we do?
- 02:08 Well, here's a list of some of the things you can check out with this.
- 02:15 You can look up the server address, the server name,
- 02:18 the type of software that's running.
- 02:21 The server protocol, request method, time, query string, document root.
- 02:29 Host, user_agent, that's a super-useful one.
- 02:33 In fact, let's go ahead and use this one right now and just take a look at it.
- 02:36 So let's create our PHP tag like we've always done.
- 02:44 So let's just echo out.
- 02:46 SERVER, and then that.
- 02:52 And like I said,
- 02:53 the thing you want to play with goes between the quotation marks.
- 02:56 In our case, we want the user agent, so let's save this, come back, and
- 03:00 hit Reload.
- 03:02 And we see this, it prints out this whole line of thing.
- 03:05 This is the user agent of the web browser that we're using.
- 03:07 I'm using the Chrome web browser, so you can see Chrome listed.
- 03:11 And this is the version of Chrome that I'm using.
- 03:14 And this is useful for web development because
- 03:17 a lot of times you'll design your website different for different web browsers.
- 03:21 You'll design it slightly differently for Chrome, slightly differently for Mozilla,
- 03:26 slightly differently for Safari that's MacOSX- type web browsers,
- 03:31 AppleWebKit, etc., etc.
- 03:34 And you want to know, when a person comes to your website,
- 03:37 what type of browser they're using so you know which type of
- 03:41 design to show them in the browser, and this is a way you can do that.
- 03:46 That's just one thing you can do.
- 03:47 Now, let's just go through here, and we're just gonna paste some of these in.
- 03:52 PHP_SELF tells us the name of the file that you're running.
- 03:57 That’s out index.php.
- 03:59 Server address, that gives the IP address of the web server, kinda interesting.
- 04:06 Server name, this is right here.
- 04:10 Kinda cool.
- 04:13 Server software.
- 04:15 What I'm doing is, I'm just coming through here and copying several of these and
- 04:18 pasting them in so we can see what each of them do.
- 04:22 So this is telling us Apache on Ubuntu, which is a Linux operating system.
- 04:26 And this is the Apache web server, version 2.47.
- 04:29 That's the SERVER_SOFTWARE.
- 04:31 Very interesting, right?
- 04:32 SERVER_PROTOCOL, see what this one is, HTTP.
- 04:40 HTTP_HOST, let's see what this one is.
- 04:44 That again, it's just telling us the URL.
- 04:47 And finally,let's look at script file name.
- 04:50 This is just another way of doing what we've already done, so save this.
- 04:55 This tells us the path of our file.
- 04:57 This is our index.php, right?
- 05:01 But this shows us the path of the web server that we're on, and we can come
- 05:05 down here to our command line and type in the PWD command that's our linux command.
- 05:09 And it tells us this is the directory we're in, /home/ubuntu/workspaces,
- 05:14 /home/ubuntu/workspaces and then the name of the file.
- 05:18 So kinda cool.
- 05:20 Those are superglobals.
- 05:22 Like I said, we'll talk about cookies and
- 05:24 sessions in the next couple of videos, but very interesting.
- 05:27 Do a little research if you're interested in that sort of thing.
- 05:30 You'll use them all the time for lots of different reasons.
- 05:32 In the next video we're gonna look at cookies.
Lesson notes are only available for subscribers.