How to make a redirect in php?

Redirect is a very powerful feature in PHP but if it is not implemented properly it can be very dangerous.

Generally, there are two ways to redirect in PHP

header()

The most common redirect is using the header() function. This is a builtin PHP function that sends raw HTTP header to the client

The Syntex

header( $header, $replace, $http_response_code);

$header = This parameter represents header string

$replace = This parameter represents new header value

$http_response_code = This parameter represents HTTP response code.

The complete example will be

<?php
 
header("Location: https://www.naveedulhaq.com");
 
exit;
?>

After the header, you should use exit or die. This is because bots and crawlers can ignore headers and they still can access the page that you want to protect using header redirect. So to stop processing you should append die() or exit() after you redirect.

About the author

Naveed Ul-Haq

I am a UK-based Technical Architect, Founder & Technology Evangelist. I'm Optimizely MVP & Optimizely SME on Content Cloud and Commerce Cloud. I love working on .NET-based CMS, eCommerce solutions, .NET Core, DevOps, and Cloud computing. I'm also a Certified Optimizely Content Cloud Developer, Certified Optimizely Commerce Cloud developer, Optimizely B2B Commerce developer and Microsoft Certified Professional in Azure application development. I spend my free time with my family and reading books. You can contact me at hello@naveedulhaq.com

View all posts

2 Comments

  • My programmer is trying to persuade me to move to .net from PHP.
    I have always disliked the idea because of the costs.
    But he’s tryiong none the less. I’ve been using Movable-type on numerous websites
    for about a year and am concerned about switching to another platform.
    I have heard great things about blogengine.net. Is there
    a way I can transfer all my wordpress posts into it?
    Any help would be greatly appreciated!

Leave a Reply

Your email address will not be published.