How to redirect after login in PHP?

by tressie.damore , in category: PHP General , 2 years ago

How to redirect after login in PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers


by adan , a year ago


To redirect the user to a different page after a successful login, you can use the header function in PHP.

Here is an example of how you can use it:

// check if the login was successful
if ($loginSuccessful) {
    // redirect to the dashboard page
    header('Location: dashboard.php');
} else {
    // login was not successful, display an error message
    echo '<p>Login failed. Please try again.</p>';

This will redirect the user to the dashboard.php page after a successful login.

Keep in mind that the header function must be called before any output is sent to the browser. If you have already sent output to the browser (e.g. by echoing a string or printing a variable), you will not be able to use the header function.

by arnoldo.moen , 10 months ago


To redirect a user after logging in using PHP, you can use the header() function to send a raw HTTP header to the browser, instructing it to redirect to a new location. Here's an example:


In this example, assume that the login logic is in the same PHP file. If the login is successful, the header() function is used to send a "Location" header with the URL of the dashboard page (dashboard.php). The exit() function is called to ensure that the script execution stops immediately and the user is redirected.

Note that the header() function should be called before any other output is sent to the browser, including HTML tags and spaces.