Mojtaba Kamyabi
Mojtaba Kamyabi

Reputation: 3620

how to use multiple headers in php with if statement

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

Upvotes: 0

Views: 1429

Answers (2)

mishmash
mishmash

Reputation: 4458

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.

Upvotes: 1

Stefan
Stefan

Reputation: 2204

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" ); 
        }
    ?>

Upvotes: 5

Related Questions