How to redirect page after login in PHP?

by dalton_moen , in category: PHP General , 2 years ago

How to redirect page after login in PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by scotty_walker , a year 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.

Member

by jerad , 9 months ago

@dalton_moen 

To redirect a page after login in PHP, you can use the header function in PHP to send a raw HTTP header to the browser.


Here's an example code:

1


In this code, session_start() is used to start a new or resume an existing session. Then, the code checks if the user is logged in by checking the value of the $_SESSION['loggedIn'] variable. If the user is logged in, it redirects the user to the dashboard.php page using the header function and exits the script.


Make sure to replace dashboard.php with the desired page URL or file path you want to redirect to after login.