课程: PHP with MySQL Essential Training: 1 The Basics

Modify headers

- [Instructor] In this chapter, we will learn about page headers and page redirects. Page redirects in particular are an essential web development skill. We're going to begin by learning about headers and how to modify them. Let's begin by reviewing the way in which a web server processes a request that comes from a browser. A request goes from the browser to the web server software. The web server software looks for the correct PHP file. It then reads that file, processes the PHP code that's inside, assembles an HTML response, and sends it back to the browser. The web server also sends a small amount of data just before the HTML response, which provides basic information to the browser about what to expect from the content to follow. It's similar to the cover page sent with a faxed document, telling how many pages total to expect in the fax. This data is called the HTTP header, and every response that uses the HTTP protocol has one. Here's an example of an HTTP header. You can see it says the protocol is HTTP. It also says the status code is 200 OK, indicating that the request was handled without errors. It has the date, some information about the server, and information about the type and the size of content to follow. Usually, you don't see or even need to think about this header information. It's automatically constructed by the web server. But we can also use PHP to give instructions to the web server on how to construct this header. We do that using PHPs header function. It expects a string as an argument. That string can be any line that we want added to the header. If the line contains some standard information, then it's going to be used in place of the default settings. For example, I can tell it that the content type should not be HTML, but that we're about to send a PDF file. Another common use is to respond to the browser with an error message. Instead of saying that the status is 200 OK, you can say that the page was not found with a 404 error or that something went very wrong with a 500 error. You can also use headers for more advanced techniques, like sending attachments and setting page cache controls. We're not going to talk about those here. There's an important point to remember about headers, and that's that headers come before all other page data. Remember, they tell the browser, hey, browser, here's what data is coming next. And they're required by the HTTP standard to proceed any communication. That means that any changes we want to make to the header has to be done before we output anything from our file. If we send even a single character to the user's browser, then the header file is already on its way out the door, right ahead of that data. At that point, it's too late to make changes to the header information, and I mean anything, even if it's a blank space or a line return. Those count, and you have to watch out for spaces and line returns that might be in included files, as well, as space is allowed inside a block of your PHP code just as long as that code doesn't actually output anything. We're going to revisit this topic again when we talk about output buffering later in this chapter. For now, just remember, headers must come first, and any changes we make to headers must come before we output any data. Let's try modifying our headers in our project file by going into our functions.PHP and creating a couple of new functions that will send error messages, error 404. And we'll make another one down here that's going to be function error 500. So in order to return a 404 error, we need to use that header function, and we need to provide a string that's going to tell it what it should return. Now, we saw that we could provide the protocol HTTP/1.1, et cetera, but a better way to do that, since the protocol could potentially change, is to use the superglobal for server and then ask it what the current server protocol is. So we'll just be repeating back whatever that current server protocol is, and then we'll append onto that 404 not found. After that, we're just going to call exit. Now, you could render your own 404 page here instead. Exit is just going to quit. It's going to say, you know what, don't do any additional PHP, we're done. Just send what you've got back to the browser. You could have something that would actually render a PHP page here instead so that you would have something to show the user that was custom. We're not going to do that. And then I'm just going to copy this two lines down here, but this time, instead of 404 not found, it's going to be 500 internal server error. Okay, so now whenever we call these functions, it will change the header and immediately exit our PHP. Let's create a way that we can try those out. I'm going to go into my subjects directory, and I'm just going to create a new file in there, which I'm going to call new.PHP. Eventually, new.PHP is going to do more interesting things. For now, we're just going to use it to test out these errors. And I also want to grab this line from the top of my index.PHP, where I get initialize, 'cause I want to have that, as well. I want to make sure that I load in those functions that we just created. So that's going to be on my new.PHP page. So now let's add some PHP code. PHP, and let's set a variable test equal to the value sent in the URL for test, and we'll use null coalesce operator to set it to a default value if it doesn't exist. And then we'll check, if test is equal to 404, then let me just close that parenthesis, and then type the curly braces. I'm going to call error_404. That's that function I just created. And then I'm going to copy that, and let's add else if test is equal to 500, then the error will be the 500 error, else echo, no error. And then let's close our PHP tags. Okay, now remember, we said that before we can modify header data, there can be no white space. Well, guess what, look at this right here. I have white space that's right here after the require_once. I want to remove that. I also still have white space because I have a line return in between those. That can count, too. So I'm actually going to remove that, as well. My PHP tag now is going to open right here at the beginning. The very first character of the file is opening up the PHP tag, and then it ends at the end. There's no extra white space before we get to having those headers. White space that's inside these PHP files that don't actually output anything, they're not a problem. It's only once we actually output to this page that it's an issue. All right, so let's save it, and let's try it out. I'm going to go into my subjects area, and instead of index.PHP, I'm going to just change this to be new.PHP. So we get no error to start with. Now, I'm just going to change this to be test 404. Now it returned a 404 error, but that's really hard to see in the browser because we didn't provide a custom 404 page back. So I just want to show you a way that we can handle that. I'm going to copy this URL. You don't have to follow along with me, but I just want to show you that in my command line, there's a tool called Curl. And if I tell Curl to use the --head option, it will let me look at the headers that come back. Let's first look at the header when I don't provide anything. You'll see it gives me back a basic header here, HTTP 200 OK. Now when I go back and I put in test equals 404, you'll see it gives me a 404 not found error. It modified the header. And let's do the same thing. We'll test out our 500, and you'll see that we get back that 500 server error. Now, those can be handled in a variety of different ways. We could have a custom error page we returned. Our web server can be configured to have a error page that it returns. Sometimes even the client, the browser, can have something configured. Now that we know how to modify the header information, we're ready to talk about page redirection in the next movie.

内容