There are times when you decide to write a redirect script in PHP. The following are the advantages of using creating such a script in PHP: 1.) PHP is a server side scripting language; the redirect script will be executed on your server and not on your visitors browser. This is a non-client/browser dependent approach. Some redirects are client side, such as JavaScript and will fail if the user wishes to disable Java Scripting functionality. 2.) It is a more flexible and versatile approach; you can execute several types of redirects in PHP that you can't with other methods. For example, the following are the most commonly types of redirects that can be done in PHP:
There are still other redirection header status' that you can do with PHP, but this tutorial will focus on the above types of redirecting URLs. This is helpful for beginning webmasters who are looking for ways to implement URL redirection if it is not possible to with other common solutions, such as .htaccess. The PHP Header() Function Redirecting URLs in PHP centers around the PHP Header() function. For example, suppose you want to redirect to this URL http://www.somewebsite.com/target.php. In the originating PHP page, you will simply call this script:
This is the most basic form and may not be appropriate in all cases or in your own implementation. Anyway for the purpose of introducing you to redirecting URLs in PHP, try this simple experiment in your XAMPP localhost: 1.) Open a text editor and type this code:
Save it as targetpage.php and put it inside the htdocs folder. 2.) Open another blank text file and enter this code:
Save it as originatingpage.php and put it also in the htdocs folder. 3.) Now launch your web browser. In the browser address bar, type: http://localhost/originatingpage.php 4.) You will notice that after you press enter. This URL: http://localhost/originatingpage.php redirects to http://localhost/targetpage.php and the content you see is the targetpage.php content which is “Hi this is codex-m”. Let's try to analyze what happens internally in the server header status sent and received by your browser. You need to have Firebug: http://getfirebug.com/ installed in your Firefox browser to do this. 1.) In your Firefox browser, paste the originating page URL: http://localhost/originatingpage.php in the address bar and do not yet press enter. 2.) In the browser, go to View and click “Firebug”.
Let's dissect what happened. First, the browser requested this URL: http://localhost/originatingpage.php to the server, then the server executed the code inside originatingpage.php. Since, it contains this line:
The server responded by redirecting the user to http://localhost/targetpage.php. You also observed that the redirection status is 302 found, which means it is a “temporary” type of redirection. After redirection, there are no further redirections and the page is given with a 200 OK status. Lessons: 1.) By default, this line:
Returns a 302 found redirection status. 2.) After redirection, the page is given with a 200 OK header status. Mistake: Outputting HTML or even spaces before Header() Now you have a very basic working knowledge of how to use header() to redirect URLs. Let's go deeper to illustrate a common pitfall when doing redirections. Try this experiment: 1.) Go to originatingpage.php script and add any HTML tag before:
Suppose you have this code:
2.) Save the file.
5.) Upload originatingpage.php to your remote hosting website root directory. Warning: Cannot modify header information - headers already sent by (output started at /home/phpdevel/public_html/originatingpage.php:6) in /home/phpdevel/public_html/originatingpage.php on line 7 What's going on here? The cause of the problem is that you already output HTML before header() function. In this case, this are the HTML you already outputted based on the above example:
Lessons: 1.) Some errors pertaining to the use of PHP header() to do redirections will not be visible or experienced in the development server (such as the XAMPP example above). But this will introduce serious issue when executed on your actual website (at the remote server). 2.) You should never output even a single HTML character, tag or text before the header() function. This includes spaces. Examples below will NOT work: a.)
Note: You outputted <p> tags before header. b.)
Note: You have a space before the PHP opening tag. c.) Some weird things will also happen if you save the updated PHP file as UTF-8, because it will introduce this character before <?php: ο»Ώ , this is called “BOM”, make sure you double check this.
blog comments powered by Disqus |
|
|
|
|
|
|
|