How to redirect to another page using PHP

前端 未结 12 1399
粉色の甜心
粉色の甜心 2020-12-02 16:50

I am building a website which includes a login page. I need to redirect the user to their profile page once they\'ve logged in successfully, but I don\'t know how to do that

12条回答
  •  南笙
    南笙 (楼主)
    2020-12-02 17:24

    You could use a function similar to:

    function redirect($url) {
        ob_start();
        header('Location: '.$url);
        ob_end_flush();
        die();
    }
    

    Worth noting, you should always use either ob_flush() or ob_start() at the beginning of your header('location: ...'); functions, and you should always follow them with a die() or exit() function to prevent further code execution.

    Here's a more detailed guide than any of the other answers have mentioned: http://www.exchangecore.com/blog/how-redirect-using-php/

    This guide includes reasons for using die() / exit() functions in your redirects, as well as when to use ob_flush() vs ob_start(), and some potential errors that the others answers have left out at this point.

提交回复
热议问题