Jump to content

php redirect using header() resulting in "page is not redirecting properly"


Adamhumbug
Go to solution Solved by Strider64,

Recommended Posts

Hi All,

I have a very simple piece of php that is set when a user logs on - it sets a sessin var.

At the top of the page i have the following:

if (session_status() == PHP_SESSION_NONE) {
	session_start();
}
if(empty($_SESSION['logged-in-user'])){
	header("Location: index.php");
	exit;
}

If i comment out the header line the page loads fine, if i dont comment it i get the error message that the page is not redirecting properly.

This seems too simple to be wrong but apparently it is - can anyone offer any light on this.

For clarity the page that i am redirecting to is the page that this code is on, i am redirecting to the same page (the log on page)

Thanks as always.

Link to comment
Share on other sites

  • Solution

If you want to keep the login form on the same page, you can modify the code to only redirect when necessary. For example, you can check if the user has submitted the login form and then only redirect them if the login fails. Here's a simple example:

if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

if (isset($_POST['submit'])) { // Assuming there's a submit button in the login form
    // Validate user credentials, and set $_SESSION['logged-in-user'] if successful
    // ...
} else {
    if (empty($_SESSION['logged-in-user'])) {
        // Show the login form and any error messages here
        // ...
    } else {
        // Redirect to a different page, or show content for logged-in users
        // ...
    }
}

This code will only show the login form if the user is not logged in and hasn't submitted the form. Once they submit the form and successfully log in, the page will show content for logged-in users or redirect to another page as desired.

Link to comment
Share on other sites

By the way, if it wasn't clear, when your browser says "not redirecting properly" what it really means is that you created a redirect loop: the page was redirecting back to itself over and over again until the browser decided enough was enough.

Conditionally redirecting would solve this, of course, by virtue of the fact that it won't redirect at all. But it does mean that you aren't completing a POST/Redirect/GET cycle. Which could be fine for you...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.