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

Mojtaba Kamyabi
  • 3,167
  • 3
  • 27
  • 48

2 Answers2

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" ); 
        }
    ?>
Stefan
  • 2,084
  • 1
  • 23
  • 36
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.

Community
  • 1
  • 1
mishmash
  • 4,620
  • 3
  • 32
  • 56