How to redirect page after login in PHP?

by dalton_moen , in category: PHP General , 9 months ago

How to redirect page after login in PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by scotty_walker , 3 months ago

@dalton_moen 

To redirect a page after login in PHP, you can use the header function to send a new Location header to the client's web browser. This will tell the browser to request a new page from the server.


Here's an example of how you can use the header function to redirect a user to the home page after they have logged in:

1
2
3
4
5
6
7
8
<?php
  // Check if the user is logged in
  if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true) {
    // Redirect to the home page
    header('Location: /home.php');
    exit;
  }
?>


In this example, we are using the isset function to check if the $_SESSION['logged_in'] variable is set and is equal to true. If this condition is met, we know that the user is logged in and we can redirect them to the home page.


It's important to note that the header function must be called before any output is sent to the browser. This means that you should make sure to call the header function before you output any HTML or other content.


I hope this helps! Let me know if you have any other questions.