Home PHP How to redirect a page in PHP?

How to redirect a page in PHP?

Author

Date

Category

To redirect a page in PHP, you can use the header() function, which sends a raw HTTP header to the browser. Here’s an example of how to use header() to redirect a page:

<?php
// Redirect to a different page
header("Location: http://example.com/new_page.php");
exit; // Make sure to call exit or die after the header function to prevent further code execution
?>
PHP


In this example, the header() function is used to send a Location header with the URL of the page to redirect to. The exit statement is used to stop the script execution immediately after the header is sent, as sending a header does not automatically stop the script execution.

Note that the header() function must be called before any output is sent to the browser. If there is any output before the header() function, it will cause a warning and the redirect will not work. So make sure that there is no output before calling header().

8 COMMENTS

  1. If some one desires expert view regarding blogging and site-building
    after that i suggest him/her to pay a visit this
    website, Keep up the pleasant work.

  2. Wonderful blog! I found it while surfing around on Yahoo News.
    Do you have any suggestions on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!
    Appreciate it

  3. That is a really good tip particularly to those fresh to the blogosphere.
    Simple but very accurate info… Thank you for sharing this one.
    A must read article!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Subhash Shipu

PHP Expert

Hey there! I'm a PHP geek on a mission to blog my way through the coding chaos. When I'm not chasing semicolons, I'm busy cuddling my pet Coco, who thinks debugging means chasing her own tail. Join the fun!

Subscribe

Recent posts