Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In the movie following this one, I want us to talk about page redirection. But in order to do that, we first need to talk about headers because headers are the mechanisms that we'll use for page redirection. We're going to get into some technical details here. But bare with me because this is important to understand and there is a payoff at the end when we learn how to do redirection. To understand headers, let's go back to the diagram of the request response cycle that we looked at early on. When our web server responds to a browser request, we know that it sends back an HTML page. It also sends data just before the page that provides basic information to the browser about what to expect from the data that's going to follow.
This data is called the HTTP header. And not only does it go from our web server to the browser with a response the browser actually send and http header with its request to the web server to begin with. Its because the header is part of the http protocol that all communication on the web uses. Here's a common example of an http header In the first line it tells us what version of http protocol it's using. And then it gives us a status code. This is very important. This number indicates whether or not this was a successful response or not.
200 means it was. That the page did successfully load. Other common status codes you would see here, would be 404 which is for Page Not Found. You're probably familiar with that. And then 500 would be for errors, things that have gone wrong. There at list a dozen other ones. But those are the three main ones that you would get there. And then you can see after that we have pairs of data. We have a label or an attribute followed by a colon, a space and then a value for it. So we have the date and we have the server, the content type, content length and then the fact letting us know the connection is no longer open.
Now usually you don't have to think about or even see this header information. It's automatically constructed for you by your web server when it responds to the request. But we can use php to give instructions to the web server to tell it how we want it to construct the header. We do that by using the header function. So header followed by a string. So for example, header, Content Type: Application PDF. Here we would be telling it, hey when you send this back, this is not HTML that you're sending, it's a PDF. You should treat it like a PDF when it arrives, and in fact if your browser has configuration options, it let's you decide how do you want to handle content that comes back as application PDF. You can also tell it that it's going to be an attachment and you can suggest a file name for what that attachment ought to be or we could send status codes like we talked about the 404.
And the 500 status codes that we could send back. Notice that those don't use that colon format, they just have HTTP and then a space followed by the status. Now here's the super duper important thing that you have to remember about headers. They come before all other page data. Remember they tell the browser here's what kind of data is about to come to you. And are required by the HTTP standard to proceed any communication back and forth. That means that any changes that we want to make to the header have 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 is already on its way out the door right ahead of it. And at that point, it's too late to make changes to the header information, and I mean anything, even a blank space or a line return. Those count. And you also have to watch out for spaces and line returns in included files; those can cause you problems. It can come inside of a php block that includes white space. That doesn't matter. As long as the php doesn't output anything to the HTML. It's okay to turn on php tags, then have some white space. And then do your redirect.
That's not an issue. It's only HTML. Things that would be output to the browser, that would warrant a response. Back to the browser from the web server. At that point web server will already have started constructing its response back. Let's try an example. I'm going to open up basic.htm and we'll do save as. And let's just call this headers.php. Headers. And if we want to change the header All we have to do is say php, and then header http colon space 1.1 slash 404 not found semicolon.
Okay, but what's the problem with this? It's not the first thing. By the time that we get here the web server has already read all of this and already started processing it. Let's go ahead and save it and bring it up and try it. We'll go back here to Firefox and in the Sandbox we're going to load up headers. Warning can not modify header information. Headers already sent by and it tells us the output started here.
So, that's the warning that you're going to get, that's the common thing that you'll see, headers already sent, cannot modify that information. And that's why this has to be the very first thing. So let's move it up here at the top. Actually I'll leave this one here, just so you see, this won't work and then let's just paste it up here at the top. And now there's no space here at all, the first thing is the php tag. Spaces don't matter, it's only the fact that we don't have any HTML before it. Let's save it and let's go back and reload the page. It happily renders the page for us now, now if you didn't have that happen, if in fact it worked for you down here, it may be that you have output buffering turned on.
So we'll come back and talk about that. For now though let's assume that we all have it off and let's put it up here at the top. Now, how do we see this? We sent it, it came back but we don't have a way in our browser really to see it. You can use Firebug. It's a nice tool that goes with Firefox. You can see it that way. But PHP also lets us see it. I'm going to use pretags and I'm going to call a function here. We don't really need to remember this function because you will never ever need it again. But it is useful just in this one context and that is headers_list. And that will return a list of the headers to us. And I'll use print r with that.
Oops put parenthesis around it. There we go. And that is going to return, then, an array, that shows us what we're getting back as the headers. Now, the headers here did not include the status, that status wasn't one of those, but we can change something else about it. Let's say header, it reported back my version of PHP. That's something that's important to note, that it does tell other web server what versions of PHP you're running. A common thing that people do is change it, so that it doesn't. There we go. And we could say, x powered by none of your business.
So there we go. Let's come back and let's reload the page. X powered by none of your business. So that's it. That's all there is to being able to set headers. The most important thing is just remembering that no space can come before those headers, unless you have output buffering turned on. Now, we're not going to be setting headers very often. It's really a rare kind of thing that you'll do, in this context. But there's one time that we do do it a lot and that's for redirects, and that's what we'll talk about in the next movie.
Get unlimited access to all courses for just $25/month.Become a member