Get your next remote job on LaraJobs.
PHP

Effortlessly redirect to another page using PHP

Benjamin Crozat
Modified on Sep 19, 2023 0 comments Edit on GitHub
Effortlessly redirect to another page using PHP

How to perform a redirection

To redirect to another page using PHP, run the following code:

// We make sure no text is echoed before we set the header.

header('Location: https://example.com');

exit;

Understanding the code

  1. We make sure no text has been echoed before setting the header. We don’t want to pollute our response. Continue reading if you want to know what I’m talking about.
  2. Then, using the header() function, we set a header that will inform the visitor’s browser that its user needs to be redirect to another page. What’s a header? I’ll explain later.
  3. Finally, we stop the execution of the code using exit. This isn’t mandatory, but this isn’t necessary either. It’s just common practice.

Basically, using HTTP, the server running your PHP code respond to your visitor’s browser and asks it to redirect its user to the URL you provided.

The anatomy of a HTTP response

Let me show you an example HTTP response, which is how a web server communicates with the browser:

HTTP/1.1 302 Found

Content-Type: text/html
Location: https://example.com

<html>
    <p>This is a redirection.</p>
</html>

Weird, right? But this is easy. Here’s how a response is constructed:

  1. The status line containing:
    • The version of HTTP used by the server.
    • The status code.
    • The reason phrase for the status code.
  2. The headers. We can see our Location header holding the URL to which we want to redirect the visitor.
  3. The body, carrying the HTML of your web page. For a redirect, HTML is usually not needed, though.

Also, do you remember when I talked about not polluting our response? Well, echoing text before using the header() in PHP will add whatever text you like in it, which can have side effects.

301 or 302? How to choose the kind of redirection with PHP?

The header() function in PHP lets you choose which kind of redirect you perform. By default, it’s creates a 302 Found redirection:

header('Location: https://example.com');

Here’s the HTTP response sent on my local machine:

HTTP/1.1 302 Found
Server: nginx/1.25.1
Date: Tue, 19 Sep 2023 18:08:31 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: close
X-Powered-By: PHP/8.2.10
Location: https://example.com

To make it a 301 Moved Permanently kind of redirection, you can leverage the header() function third argument: $response_code:

header('Location: https://example.com', response_code: 301);

Which sends a HTTP response that looks like this:

HTTP/1.1 301 Moved Permanently
Server: nginx/1.25.1
Date: Tue, 19 Sep 2023 18:13:01 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: close
X-Powered-By: PHP/8.2.10
Location: https://example.com

Wait, there's more!

Be the first to comment!

Get help or share something of value with other readers!

Great deals for enterprise developers
  • ZoneWatcher
    Get instant alerts on DNS changes across all major providers, before your customers notice.
    25% off for 12 months using the promo code CROZAT.
    Try ZoneWatcher for free
  • Quickly build highly customizable admin panels for Laravel projects.
    20% off on the pro version using the promo code CROZAT.
    Try Backpack for free
  • Summarize and talk to YouTube videos. Bypass ads, sponsors, chit-chat, and get to the point.
    Try Nobinge →
  • Monitor the health of your apps: downtimes, certificates, broken links, and more.
    20% off the first 3 months using the promo code CROZAT.
    Try Oh Dear for free
  • Keep the customers coming; monitor your Google rankings.
    30% off your first month using the promo code WELCOME30
    Try Wincher for free →
The latest community links
- / -