Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
PHP is a popular, reliable programming language at the foundation of many smart, data-driven websites. This comprehensive course from Kevin Skoglund helps developers learn the basics of PHP (including variables, logical expressions, loops, and functions), understand how to connect PHP to a MySQL database, and gain experience developing a complete web application with site navigation, form validation, and a password-protected admin area. Kevin also covers the basic CRUD routines for updating a database, debugging techniques, and usable user interfaces. Along the way, he provides practical advice, offers examples of best practices, and demonstrates refactoring techniques to improve existing code.
Alright. So in the last movie we took a small detour so that we could understand what headers are and how they work. During that detour, hopefully I impressed upon you the importance of altering headers before even a blank space is output to the browser. Now, we're ready to talk about when you'll need to work with the headers most often, and that's when we want to do Page Redirection. That's where they're really useful. So what is page redirection, and why do we need it? Well, let's say that the user goes to a log in page, they submit their log in information on a web form, gets sent to a PHP page for processing. If they succeed in logging in, we want to send them to one page. If they fail, we want to send them to another page. That's where redirection comes in, being able to send them to another page. A page that's different from the one that they requested, or even the one that the form submitted to.
I'm sure you've seen this in e-commerce. You submit your order, and then after a brief pause, you suddenly are redirected to another page. Your browser suddenly ends up with a different URL at the end, that says something about your receipt, has your order number or something like that in it. When that happens, you've been redirected. In HTTP, the standard that the web uses, we can redirect the browser to a new URL by using a 302 redirect. It has two parts: a status code of 302 found and a location attribute that indicates the new URL.
We saw in the last movie how we can change the header. Well, PHP is smart enough to know that if we're setting the location in the header, then we also want to set the status code to 302 at the same time. We don't have to manually set it and make a two-header request, we can just do it with one. It's really nice. Now, notice "Location:" call in space, and the capital "L" in location. It has to be exactly like that, or it won't work. Capital L location: call in space and then the new URL. A 302 status code in the header when it's received by the browser, tells the browser that it should immediately make a new, second get request to the new location.
It ignores any page data that follows the header, in fact it expects that there won't be anything else. The re-request happens very fast in the background, so the user doesn't even know it's happened. But it actually is a second get request that takes place. Let's try it. So, let's try to open up basic.html and let's do Save As on it, and we're going to call this redirect.php. So, let's put it at the top here, remember we already know that it has to go at the very top, no space at all. We'll open our PHP tags, and this is how you redirect to a new page. Header, open our parentehse, open our quotes, capital L Location: call in space and then the page that you want it to go to.
I'm just going to use basic.html. That's going to redirect to basic.html. Now, this can be a relative path to the file, which is what I'm doing here. I'm saying it's basically in the same directory, or I could provide everything. I could say, this should go to http:www.lynda.com, put a full URL there. Now, if we try this as it is, what'll happen? Well, the redirect will be sent here, but there's also data that's being sent afterwards. And we don't really want to send any data after that. So, the last step is that you always want to do exit afterwards. So, exit is going to tell it, we're done. The script is over, the PHP script is finished now, we've done the redirect. That's it.
And that's because we don't need anything else. Once we've done a redirect, that's it. That's the whole response that's going to be sent back. So let's try it. And we should change then this to Redirect, just for completeness. And let's go back to Firefox, and let's just try redirect.php. And there we are. It redirected, notice now the URL is basic.html. That's where it went to, untitled is the title of that page. And if we do web developer view source, then we'll that there's the content of the basic.html page. So it did send me immediately to it again.
If you want to just try it a few times, you can watch it happen. Redirect.php, and there it is, it happens very, very quickly, especially since we're on the same machine, not even reaching out across the internet. But even then, it happens very fast. It's not, the speed of making a second request to something you really don't have to worry about. Now, instead of trying to remember the, the syntax here for remembering that you have to call header, you have to put location and it has to be exactly like this, that you have to call exit, I find it really helpful to wrap all of this up into a function. So, let's create a function for ourselves.
Function, and we'll call it redirect_to, and it's going to take as an argument new location. And then we'll open our curly braces there, I'll just indent all of this, it looks nice. And then of course instead of putting basic.html here, it's going to just append new location. Now, we can just call redirect to and that will do the redirect for us. Just try here, redirect_to. And let's go ahead and just give it basic.html.
Here we are. Come back here. Redirect.php, basic.html. And send us back there. I find this really helpful. I find this much easier to use, and user-friendly than trying to remember that I have to do things like add exit after. So let's just use this in some PHP. So let's say that we're going to a value for logged in, and that's going to be equal to get, and we'll say logged in. So the value of get logged in is what will become logged in, and then let's do a simple if statement. If $logged_in is equal to 1, then we'll do one thing, otherwise, we'll redirect somewhere else.
If they are logged in, we're going to let them see basic.html. If they are not logged in, let's redirect them to somewhere else, like lynda.com. So you can see what it looks like when you go to a different URL completely, that works just as well. Save it. Alright. So I definitely have to send a value for logged_in here. I'm not doing any kind of default value, so let's make sure that I send that. Redirect.php logged in equals 1. There it is, sent me to basic.html. Let's try again. Redirect.php and logged in equals zero. Really anything would work.
And you see that it brings me up to the lynda.com page. So that's it. You need to know this syntax, which I think you can roll up into a nice easy to use function. And that you can't put any space before it. If I try something like this, then even if I am logged in, redirect.php logged in equals 1. I still get that warning, cannot modify header information, because something has already been sent. Even though it's just a line return, something has been sent before the headers. The only way that we can get around that is by using output buffering, and that's what we'll talk about in the next movie.
Find answers to the most frequently asked questions about PHP with MySQL Essential Training.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.