0

i want to redirect login.php to index.php when $_SESSION['user'] is not empty (user logged in)

<?php
    session_start();
    if (isset($_SESSION['user'])){
        header ('refresh:3 ; URL:index.php');
    }
?>

but when user log in the page doesn't redirect to the index.php

2 Answers 2

5

This should work:

<?php
    session_start();
    if (isset($_SESSION['user'])){
        header('Location: http://www.yoursite.com/');
        die();
    }
?>

If you want to redirect the user after x senconds, then use

    <?php
        session_start();
        if (isset($_SESSION['user'])){
            header( "refresh:3;url=whatever.php" ); 
        }
    ?>
Sign up to request clarification or add additional context in comments.

5 Comments

Too bad it doesn't do what he wants. :P
i want to use refresh too in3 seconds but when i write header('Refresh:3;Location: index.php'); it doesnt work why ?
@anonymous, why are you trying to do it that way? The answer was just given to you. Use it correctly as given and don't modify it so it does not work.
Look at my answer real hard. I even pinpointed the problem with a little ^.
@anonymous you are mixing up the two possibilities, have a look at my second example, that's what you are trying to do.
1

You're doing it wrong. Example of how to do it and some more info about the header.

<?php
session_start ();
if (isset($_SESSION['user'])
{
    header ('Refresh: 3; url=index.php');
    //                      ^
}
?>

You used : it should be an equal sign.

5 Comments

You shouldn't use die when using 'Refresh' header.
thanks when i write Location instead of URL this works fine. but when i write url this doesnt work.
Are you even trying? Look harder... Your code was OK, just one character was wrong. You can do it!
thank you mistake is from me :D when i refresh the page this works because i dont define $_SESSION['user'] in proper position.
It might have been that this time, but on some browsers this wont work: Refresh: <seconds>; url:<page> You are better of using: Refresh: <seconds>; url=<page>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.