How to Make a PHP Redirect Page

There are numerous reasons to redirect visitors to your website. For example, if you change the location of a page, it makes sense to replace the old file with a script to redirect visitors to the new page. If you want to display a message to visitors when they click a link away from your website, you can create a redirect page to achieve this purpose. In addition, if a visitor does not have permission to view a certain page on your website, a redirect can be used to display a "Permission Denied" page. PHP, the popular web programming language, can be used for this purpose.

Instructions

    • 1

      Open a text editor, such as Notepad, and create a new page. Use PHP's "header" function to send out the redirect HTTP header "Location: http://www.website-to-redirect-to.com." Ensure that there is no output before the HTTP header is sent, including white space outside the PHP tags, as it will cause an error. Here is the code for this step:

      <?php

      header("Location: http://www.examplesite.com");

    • 2

      If you want to display a message to users before redirecting them, use the "refresh: x" and "URL=http://www.someurl.com" headers to make the script wait "x" seconds before redirecting the user. After you send the header, you can display some text to the user. For example:

      <?php

      header( "refresh:5;URL=wherever.php" );

      echo "You will be redirected in 5 seconds";

    • 3

      If you want to redirect users based on a "GET" parameter, a parameter in the URL, such as "redirect.php?URL=http://www.google.com," you can use PHP's "$_GET" array to retrieve the URL and redirect accordingly. Here is the code:

      <?php

      header("Location: " . $_GET['URL']);

    • 4

      Save the PHP file. Make sure that the file is saved with a ".php" extension, or else it will be interpreted as a text file and will not run. In Notepad this is done by choosing "File" in the menu bar, clicking "Save As," changing the "Save As Type" option to "All Files" and clicking "Save."

Related Searches:

References

Comments

You May Also Like

  • How to Send a Free SMS Using PHP

    Short message service (SMS) messages, also known as text messages, have become the preferred way of communication for many people. They offer...

  • How to Redirect a Web Page in PHP

    Unlike HTML and Javascript, which are downloaded from the Web server into the browser before being processed, server-side scripting languages like Hypertext...

  • How to Create Dynamic Web Pages With PHP

    Dynamic web pages are an efficient way to present data to the user. The web designer needs only to create the initial...

  • How to Create a File Upload Routine in PHP

    The ability to upload files on a website is a very useful feature, especially for a site designed for greater user interaction....

  • How to Create a Redirect Page

    Redirecting site visitors to an alternate Web page is useful when your main site is down or you are transitioning to a...

  • What Is the Purpose of Baffles?

    A baffle is an artificially-created wall or tunnel within the atria of the heart. It's designed to redirect the flow of blood....

  • How to Redirect URLs

    If you are launching a new website or a beta website that builds on the experience already provided by a current website,...

  • How to Create a PHP Survey

    Obtaining user feedback is a critical component to every website, whether it is used as market research for a product line, customer...

Related Ads

Featured