I have a site where I am using a name and password to login.
If logged in it sets a $_Session['valid_user'] variable to their username.
What I want to do is on pages that are member only I want to test to see if they are in a logged in state and the session variable for valid_user is set. If they aren't logged in then I want to redirect them back to a page where they have to login.
I thought the way to do it would be this:
<?php
session_start();
require_once('includes/member_fns.php');
if (!$_SESSION['valid_user'])
{
// they are not logged in send them to the login
header("Location: login.php?form_failed=1");
exit;
}
else
{
echo "<div id='valid_user'><p>Logged in as: " .$_SESSION['valid_user'] ."</p>;
}
?>
However, navigating directly to the page bypassing the login process tosses the following two errors:
Encountered error: 8 in /home/consult/public_html/OMGMA_Testing/OMGMA_1_0/member_community/authenticate/mc_directory.php, line 5: Undefined index: valid_user
Encountered error: 2 in /home/consult/public_html/OMGMA_Testing/OMGMA_1_0/member_community/authenticate/mc_directory.php, line 8: Cannot modify header information - headers already sent by (output started at /home/consult/public_html/OMGMA_Testing/OMGMA_1_0/member_community/authenticate/includes/error_fns.php:4)
It appears the session variable is undefined.
What am I doing wrong? Is there a more practical way to handle this?
Basically I am just looking for a solid way to allow members to login and access each of the member pages without having to login again. So if they login and then they can access any of the pages that are member only, but if they aren't logged in they fail and are sent away or to my login page.