Add re-direct on session log in

Hi I’m using a simple session log in script


<?php
//put sha1() encrypted password here - example is 'aka'
$password = 'loads of randomness';

session_start();
if (!isset($_SESSION['loggedIn'])) {
    $_SESSION['loggedIn'] = false;
}

if (isset($_POST['password'])) {
    if (sha1($_POST['password']) == $password) {
        $_SESSION['loggedIn'] = true;
    } else {
        die ('Incorrect password');
    }
}

if (!$_SESSION['loggedIn']):
?>


When the user logs in the page is simply refreshing without any content - ideally i’d like a re-direct to anotherpage.php to happen, I’m trying to add in after the session is loggin in.

You can use the HTML redirect technique. Just put this in your page’s header:

<meta http-equiv="Refresh" content="5; url=http://example.org">

This example will redirect the page to example.org after 5 seconds. Just put in your own URL and set the wait time to whatever is appropriate.

You can also use the header() function to redirect the browser:


if (sha1($_POST['password']) == $password) {
    $_SESSION['loggedIn'] = true;
    header('Location: http://www.example.com/anotherpage.php');
} else {
    die ('Incorrect password');
}

The important thing is to make sure you call header() before sending any other output, otherwise you’ll get an error.

Thanks Fretburner works a treat.