‫In this video we're going to look at the get superglobal, which is used to get variables that are sent
‫to your script via Get request, usually through what's called query parameters, and that is the data
‫that's sent in the URL.
‫So for example, let's just open this up real quick so I can show you.
‫You've probably seen this many times where you have a domain, and then you'll have a question mark.
‫And maybe let me use a different example.
‫So you might see something like let's say it's a search page and you need a query.
‫So you might see question mark query equals.
‫And let's say you were searching for I don't know footballs okay.
‫And then you might have another query parameter of sort.
‫So then we have an ampersand and say sort equals ascending or descending.
‫And then maybe there's a limit.
‫So you could have another ampersand and say limit equals ten.
‫So these are query params its data passed through the URL.
‫So the first query parameter always starts with a question mark.
‫And then every additional one is going to have an ampersand.
‫Okay, so the get superglobal allows us to get this data so we can get limit and we can restrict the
‫number of footballs coming from the database or whatever.
‫So let's go ahead and jump into the index.php.
‫And I'm first going to just show you how this works.
‫And then we're going to go to our job listings page.
‫And we're going to be able to filter by location.
‫So we'll be able to pass a query parameter or a query string that has the location in it.
‫And we'll get just the just the jobs for New York or whatever, whatever the value is.
‫All right.
‫So in the index.php let's just go ahead and.
‫Let's echo out.
‫Actually, you know what?
‫Before we do an echo, let's do a var dump.
‫Of just what we get from get.
‫Okay, so if I save that and I'm going to come over here, I'm going to take everything out of the URL
‫except for the the main domain localhost 8000 could be something else for you.
‫And you see that it's an array.
‫So Get is an associative array of values.
‫So if I pass in here now question mark and let's do name equals Brad or name equals whatever.
‫So now that name gets added to the associative array.
‫The key is name.
‫And the value in this case is Brad.
‫If I do an ampersand and I do age equals 30, then we get another age as a key and then 30 as the value.
‫And it's going to be sent as a string.
‫So if you do need that to be an int, if you're going to be doing mathematical operations on it, make
‫sure you do cast that to an integer.
‫All right.
‫So you always want to check first to see if it exists.
‫Because if I do for instance echo and let's say get and then get the name oops quotes name.
‫So if I do that and I reload I get Brad.
‫But if it's not here, which in many cases won't be, I'm going to get an error, a warning that says
‫undefined array key name.
‫So you always want to check first.
‫And there's a few ways you could do that.
‫You could use is set.
‫So you could do an if statement or a ternary like this.
‫So we could say is set and just wrap that.
‫If it is set then we want to echo the get.
‫Name else, then just an empty string.
‫So if I do that and reload, even though I didn't send anything in the URL, it doesn't give me an error,
‫it's just echoing an empty string.
‫You could also use a null coalescing operator, which is a little cleaner.
‫So we could just say get.
‫Name, and then just use the null coalescing and then empty string.
‫So if I do that comment that one out we get the same result.
‫And if I do add a name so name equals Brad.
‫Then I get Brad.
‫All right.
‫So that's the basics of just getting the variables.
‫Getting the values from the URL from the query string.
‫Now superglobal is like get and post.
‫They make PHP really easy to work with.
‫And they also make it more vulnerable.
‫So let me show you what I mean.
‫If we're you have to be aware of what is being sent by a user, right?
‫Because because a user can send anything in this name value.
‫And you have to think about what are you doing with that value.
‫You're storing it in a database.
‫Are you going to show it on the page.
‫So if you're going to show it on the page, for example, you have to be careful because watch what
‫happens if I say name equals.
‫And then up here I do a script tag and then I do.
‫Actually I already have it here.
‫So if I add in here a script tag and then alert one, which is JavaScript, and then an ending script
‫tag, and I'm putting that on the page that will actually run the JavaScript.
‫All right.
‫If we look at the oops, if we look at the source code, we have our JavaScript.
‫So you don't want users to be able to do that.
‫Now obviously if I'm not putting this on the page then that's not going to happen.
‫If I reload, that's not there.
‫Right?
‫If we look at the source code, there's nothing there because I'm not putting it on the page.
‫But very rarely should you take in data from a query param and just put it on the page.
‫However, if you're going to there's there's methods that you can use to prevent this from happening.
‫So for instance there's a and I'm going to get into this later when we get into our database stuff.
‫But for instance you could use HTML special chars.
‫And if I wrap.
‫If I wrap that before I put it on the page.
‫Right.
‫So if I go like that and then I reload now you'll see it's not actually executing that JavaScript.
‫It's just putting the text on the page.
‫And if we look at the source code, what it did is it turned those angle brackets into these HTML entities.
‫Okay.
‫So this would be the left angle or the yeah, the left angle bracket script.
‫Right angle bracket.
‫And it's just printing it on the page so there's no harm done.
‫So you just have to be aware of what you're doing with any data that you get from any users, whether
‫it's through query params, whether it's through a form, through a Post request.
‫That's something you always need to be aware of.
‫All right.
‫So I just wanted to mention that.
‫So what we're going to do now is use our oops use our job listings that we've been using.
‫So in the in the sandbox starter files you'll have basically what we left off with.
‫We have our job listings.
‫We have our highlight tags and format salary.
‫All this stuff we've done so far.
‫So I'm going to go to that page.
‫So I'm going to go to 8000 slash job listings.
‫And let me just make this smaller.
‫And what we want to do here is have the ability to pass in a query string of, for instance, location
‫equals Chicago.
‫And if we do that, if I do it right now, there's no change.
‫But I want it to be able to filter and only show jobs that are in the location of Chicago.
‫Okay.
‫So to start off, let's go down.
‫Let's see.
‫We'll go down below all of the functions here.
‫And let's put a comment.
‫We'll say check, check if location query query string is there.
‫And I use query string and query params interchangeably.
‫So remember we want to make sure that it's set first.
‫So we'll say if is set the get so money sign underscore get and then in quotes.
‫I should say brackets and quotes.
‫We want to check that get location.
‫All right.
‫And then let's say if oops I already have my curly braces there.
‫So if that's found then let's set a variable called location.
‫And let's set it to whatever that get value is.
‫So get.
‫Location.
‫All right.
‫Now.
‫What we're going to want to do is have a different variable that we loop through.
‫And I'm going to call that filtered listings.
‫And we're going to set that to a function called filter.
‫We'll call it filter listings by location.
‫And then that's going to take in the listings array.
‫And it's going to take in the location that comes from the get from the query string.
‫Okay.
‫And then what we're going to do is use this to filter through.
‫So down here instead of just listings we're going to change that to filtered listings.
‫Now we need to take account for if this is not there.
‫So let's say else then we're just going to set filtered listings equal to listings the original listings.
‫So if I don't have this location here right.
‫If I reload let's see undefined okay.
‫So this is undefined right now.
‫So let's just define it.
‫I'll go right above it and say function.
‫Location and that's going to get passed in.
‫Listings.
‫Location.
‫And for now, I'll just.
‫I don't know, I'll just echo out.
‫Location.
‫All right, so let's reload.
‫And you can see that if the location is there, it is echoing out Chicago.
‫And it's not going to it's not going to do anything down here because we haven't added that logic.
‫Now if we don't have the location, if I get rid of that, then we just get all of them.
‫We get all of the listings.
‫So now let's do this function here.
‫And basically I'm going to use the the the array filter function to filter out anything that is not
‫Chicago or is not whatever that location value is.
‫So let's say return array, underscore filter.
‫And this is going to take in the listings array.
‫And then the second argument is going to be a callback function.
‫So let's say function.
‫And for each iteration or each listing let's call it job.
‫So then we have access to to each job.
‫Now we also need access to this location variable.
‫So what I'm going to do is go right after this closing parentheses.
‫And I'm going to say use and then location.
‫That way we have access to it from within here.
‫And then I want to be able to to match whether it's all lowercase, all uppercase a mix.
‫So it shouldn't matter the case.
‫So I'm going to use a function called str case cmp.
‫All right.
‫So that will take in two values and it'll match it.
‫But it can it can be case insensitive.
‫So the first string is going to be the current job's location.
‫Right.
‫And then the second is going to be the location variable okay.
‫Now if that matches even if it's case insensitive then it actually gives us a zero in return.
‫So we want to check for that zero.
‫Okay.
‫So let's save that.
‫Let's come back here.
‫Reload.
‫So not adding a location we get all the listings.
‫And then if we say location equals Chicago there we go.
‫We have it filtered down to that one listing that's in Chicago.
‫If we say location New York.
‫Then we get the two, the two listings that are in New York.
‫So you can see that the get superglobal is pretty powerful.
‫Now, even though we're not outputting that on the page, like if we do, if we put a script tag here
‫and we try to do some shenanigans, we're not putting it on the page.
‫So that script tag isn't going to run.
‫Right.
‫And just to show you that, we'll say script alert one.
‫And then close that script tag.
‫So I'm just going to get nothing.
‫Right.
‫But you still want to you still want to be cautious of things like this.
‫So what we can do is have an array of valid locations and make sure that whatever is added here is in
‫that array.
‫And that's just one one extra step you can take.
‫There's a lot of things you can do to protect against, you know, protect against -- people.
‫So let's go up here and at the top right under the listings array I'm going to say valid locations.
‫And I realize you can't always do this like you might not.
‫You might allow any locations.
‫So this wouldn't work in that situation.
‫But maybe you have a select box that has certain locations and you want only those to be able to be
‫selected.
‫So let's say Chicago.
‫I have another string of San Francisco and New York.
‫And what else do we have?
‫Seattle.
‫And Boston.
‫All right.
‫So we have these valid locations.
‫Now all I have to do if I want to make sure that they're in there is come back down to let's see, we
‫want to come back down to where we check for the location query string.
‫And we just want to add on to that.
‫So let's put a double ampersand.
‫So we're saying and and then I'll use the in array function which will just check to see if something
‫is in an array.
‫And in here I'm going to pass in the.
‫Get.
‫Location because it takes in an array.
‫And then I want to make sure that.
‫Whatever is passed in there is in this valid locations.
‫So now it's going to check that.
‫So if I reload that works if I.
‫Just do like test.
‫It just shows me all of them.
‫And if I do that script that I showed you before.
‫So if I do that again, it's just going to show all of the listings and we're fine.
‫So just just an extra security step.
‫So that's the basics of Get.
‫We'll be using it much more in our project, especially when we create our router.
‫So in the next video we're going to look at post which handles post variables.