Home arrow PHP arrow How to Redirect URLs in PHP

How to Redirect URLs in PHP

In this programming tutorial, we will look at different methods to redirect URLs in PHP. Specifically, we will be examining 301 URL redirect methods via code, meta refresh and the use of PHP header location settings to invoke a redirection.

TABLE OF CONTENTS:
  1. How to Redirect URLs in PHP
  2. Permanent URL 301 Redirect in PHP
  3. Refresh Redirects in PHP
By: Codex-M
Rating: starstarstarstarstar / 3
April 27, 2011

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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:

a.) 301 permanent redirection status.
b.) 302 temporary redirection status.
c.) Refresh type

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:

<?php
header('Location:
http://www.somewebsite.com/target.php');
?>

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:

<?php
echo "Hi this is codex-m";
?>

Save it as targetpage.php and put it inside the htdocs folder.

2.) Open another blank text file and enter this code:

<?php
header('Location:
http://localhost/targetpage.php');
?>

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”.
3.) Click “Net” tab.
4.) Now, click on the browser address bar and press enter to load the page.
5.) You will notice that after pressing enter, the server header status details are showing in the Firebug Net panel. This is how it looks:

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:

header('Location: http://localhost/targetpage.php');

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:

header('Location: http://www.yourtargeturl.com');

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:

header('Location: http://localhost/targetpage.php');

Suppose you have this code:

<html>
<head>
<title>This a redirection example that will fail</title>
</head>
<body>
<?php
header('Location:
http://localhost/targetpage.php');
?>
</body>
</html>

2.) Save the file.
3.) Run again originating.php script in the browser using your development server/XAMPP localhost. If you don't see any error, you will notice that it still redirects cleanly to targetpage.php
4.) Now try changing the target URL to point to your real website, example:

<html>
<head>
<title>This a redirection example that will fail</title>
</head>
<body>
<?php
header('Location:
http://www.php-developer.org/');
?>
</body>
</html>

5.) Upload originatingpage.php to your remote hosting website root directory.
6.) Execute the script in the browser by calling the originatingpage.php URL, for example: http://www.php-developer.org/originatingpage.php
7.) You will notice that this time, you will encounter an error such as:

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:

<html>
<head>
<title>This a redirection example that will fail</title>
</head>
<body>

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.)

<p>Hi, hello!</p>
<?php
header('Location:
http://www.yourtargeturl.com');
?>

Note: You outputted <p> tags before header.

b.)

{space before <?php opening tag}
<?php
header('Location:
http://www.yourtargeturl.com');
?>

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.



 
 
>>> More PHP Articles          >>> More By Codex-M
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: