How to redirect to another page using PHP

前端 未结 12 1369
粉色の甜心
粉色の甜心 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.

    0 讨论(0)
  • 2020-12-02 17:25

    On click BUTTON action

       if(isset($_POST['save_btn']))
        {
            //write some of your code here, if necessary
            echo'<script> window.location="B.php"; </script> ';
         }
    
    0 讨论(0)
  • 2020-12-02 17:26

    ----------
    
    
    <?php
    echo '<div style="text-align:center;padding-top:200px;">Go New Page</div>'; 
    		$gourl='http://stackoverflow.com';
    		echo '<META HTTP-EQUIV="Refresh" Content="2; URL='.$gourl.'">';    
    		exit;
    
    ?>
    
    
    ----------

    0 讨论(0)
  • 2020-12-02 17:31

    You can conditionally redirect to some page within a php file....

    if (/*Condition to redirect*/){
      //You need to redirect
      header("Location: http://www.yourwebsite.com/user.php"); /* Redirect browser */
      exit();
     }
    else{
      // do some
    }
    
    0 讨论(0)
  • 2020-12-02 17:32

    You could use ob_start(); before you send any output. This will tell to PHP to keep all the output in a buffer until the script execution ends, so you still can change the header.

    Usually I don't use output buffering, for simple projects I keep all the logic on the first part of my script, then I output all HTML.

    0 讨论(0)
  • 2020-12-02 17:33

    Just like you used echo to print a webpage. You could use also do the same with redirecting.

    print("<script type=\"text/javascript\">location.href=\"urlHere\"</script>")
    
    0 讨论(0)
提交回复
热议问题