I have a login.php file:
<?php
session_start();
include('db.php');
if(isset($_POST['email']) && !empty($_POST['email']) AND isset($_POST['password']) && !empty($_POST['password'])){
$email = mysql_escape_string($_POST['email']);
$password = mysql_escape_string($_POST['password']);
$search = mysql_query("SELECT * FROM users WHERE email='".$email."' AND password='".$password."' AND active='1'") or die(mysql_error());
$match = mysql_num_rows($search);
if($match > 0){
$row = mysql_fetch_assoc($search);
$user=$row['forename'].' '.$row['surname'];
$_SESSION['username']=$user;
//$msg = 'Login Complete! Thanks, '.$user.'!';
header( 'Location: page1.php' ) ;
die;
}else{
$msg = 'Login Failed!<br /> Please make sure that you enter the correct details and that you have activated your account.';
}
}
?>
Now, when I load page1.php I have issues. The file contains this...
<?php
session_start();
include('db.php');
if(isset($_SESSION['username'])){
echo 'Success, '.$_SESSION['username'].'!';
}else{
echo 'No dice!';
//header( 'Location: login.php' ) ;
}
?>
I ideally want it to redirect to login if there isn't a username stored. If there is, I want to allow them to view. However, I am getting "No dice!" every time, so it looks like I am not retrieving (or storing) the data correctly. What am I doing wrong?
To be clear, the else shouldn't be firing as it should be referring to session data set in index.php. The redirection is not a problem.
The problem was specific to my host who had a strange setup. On contacting them, they provided me the correct path information which I had to use session_save_path to set. Awarded the right answer on this basis.
login.php?