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,440
  • 3
  • 29
  • 50

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,164
  • 1
  • 23
  • 40
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,422
  • 3
  • 34
  • 56