We will be undergoing scheduled maintenance on May 20th, 2013 at 02:00 GMT.
In this tutorial we're going to be talking about how you can redirect the browser using PHP. You'll want to do this in some pages where your script needs to execute some code and then actually transfer the browser to a different page. This happens a lot in PHP because oftentimes you'll write code on a specific page that processes a lot of information, it's your program logic. But you don't actually want to display anything off of that page, you'll want to redirect the browser and oftentimes do it conditionally based upon the results of your logic. After we talk about redirects then we're going to talk a little bit about some places where you can get more information about PHP. A couple of really good references. So let's start with Redirects. What I'm going to do is I'm going to load the 0206 Project File called 0206 Checkout.php. So here we have our framework for a web page and we're going to pretend this is our shopping cart page. We've got four items in our cart and we're going to click the Checkout Button. So normally in this type of situation you're going to allow the user to click they want to checkout and then you're going to execute some logic. And then dependent upon what's in their cart or certain criteria's that are met or values that are available, you're going to redirect the browser accordingly. So usually what you'll do is you'll go to a processing page and then you'll have that processing page take you somewhere else based upon the resulting logic. And that's what we've done here in our code. If you look in the 0206 Project Folder you'll see our HTML for this checkout page and what we need to do is we need to point this form to go to the process Payment.php. So we'll go ahead and say the action of this form is equal to the 0206 Process Payment.php and now our form will go ahead and submit the data to the processing payment page right here. So let's refresh our browser and click our button and now you could see we're at the Process Payment.php page. So here is where we would enter the logic for handling the checkout process. And then after we're done with that logic you'll want to redirect to a different page. This all happens behind the scenes, they never see any of the code that you put in this process payment. So we'll go ahead and say that they processed the payment and now we want to redirect them to the receipt page. So redirect to the receipt and how you do that in PHP is you use a Header Function. So the Header Function will allow you to output any one of the standard HTML or HTTP Headers to the browser. So we'll say Header and then there's a directive called Location and you'll just use this directive every time you want to redirect to a specific location. And after that you'll just enter in the URL. Now it could be dynamically generated URL, you know, a variable whatever you want to put in there, the result needs to be a text though. So we want to redirect them to our receipt page which is right here, the 0206 underscore receipt.php. So we'll go ahead and just copy the name of that file and we'll paste that up here. And now we'll have a redirect. So after processing our logic it'll take us right to the receipt page. So let's see what this looks like. I'll go ahead and back up and checkout again and this time you didn't see any results from the logic that was executed on the process payment page. We were redirected directly to Receipt.php. So that's how you can do redirects in PHP. One thing you do need to note is that you don't want to have any output even if it's just empty text sent to the browser ahead of the headers. So this violates the HTTP protocol and you want to make sure that there's no strings or no output and you don't want any blank lines either. So above your PHP declaration you wouldn't want an empty line because that's empty string. No output to the browser before your header. Alright. So let's talk about a couple of references. The one which goes without saying is definitely the www.php.net website. Here is the vast library of documentation, function reference and explanations on how to use PHP. So we just talked about the Header Function. If you want to know more about that you could search setup here and you would find everything about this Header Function. And all the specifications and the different directives and the things that you can do including user comments down below the function declaration. And what other users have found about using this function, very helpful. And another reference I'll give you are the good people at WWW.W3Schools.com. Because here they're going to show you all different kinds of references and materials not just on PHP but HTML, CSS, JavaScript and so forth. And of course on server side scripting they do have a link for PHP.
| Course: | FileMaker and PHP Foundations |
| Author: | Lance Hallberg |
| SKU: | 34393 |
| ISBN: | 978-1-61866-077-0 |
| Release Date: | 2012-12-07 |
| Duration: | 7.5 hrs / 97 lessons |
| Work Files: |
Yes |
| Captions: | No |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |