when i click login.. it doesn\'t automatically redirect me to home.php, i have to refresh the page for it to redirect me. I\'m guess the first header() is working fine bc it
A header()
function call sends a header to the browser telling it where to go, but it does not stop the execution of the PHP script. So after a header()
call used for redirection you should in almost all cases put an exit;
directly after it.
<?php
session_start();
if($_SESSION['user']!=""){
header("Location: home.php");
exit;
}
Also on your second header()
call.
Additional note
You should also be checking that $_SESSION['user']
exists before attempting to test it like this
session_start();
if( isset($_SESSION['user']) && $_SESSION['user']!=""){
header("Location: home.php");
exit;
}
This needs doing whereever you test for the user
in the session as if they are not logged in it wont actually exist.
This is one of the problems with learning PHP on a LIVE server where the display of errors is turned off. So while you are learning or even after, while developing a script on a live server remember to add these 2 lines to the top of the scripts you are developing
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
But then remember to remove then when a a script is working as its bad form to show a user this kind of error.