I hate to post another question about sessions but nothing here seem to address my particular problem.
Here's my code
<?
session_start();
$sid = session_id();
$nerror = "";
$perror = "";
if(isset($_POST['Logout'])){
session_destroy();
}
if(isset($_POST['Login'])){
$userid = $_POST['userid'];
$userpass = $_POST['userpass'];
if(!$userid) {
$nerror = "Please enter a valid Username!";
}
if(!$userpass) {
$perror = "Please enter a valid Password!";
}
else {
$userpass = md5($userpass);
}
$location = "localhost";
$username = "ecgc_dbadmin";
$password = "e1c5g0c5";
$database = "ecgc_Press";
$press_db = mysql_connect("$location","$username","$password");
if (!$press_db) die ("Could not connect MySQL");
mysql_select_db($database,$press_db) or die ("Could not open database");
$user_qry = "
SELECT *
FROM Users
";
$user_res = mysql_query($user_qry);
while ($user_row = mysql_fetch_array($user_res)){
list( $record_number, $Name, $User_Id, $User_Pass, $Email, $Logs, $admin, $home, $aword, $tracts, $booklet, $gospel, $outlines, $commentary, $reviews, $testimonies, $contact, $directions, $serving, $ecbts, $tvshow, $university, $bible, $recordings, $phone ) = $user_row;
if($userid == $User_Id){
if($userpass == $User_Pass){
session_register('Name');
$_SESSION['Name'] = $Name;
session_register('User_Id');
$_SESSION['User_Id'] = $User_Id;
session_register('admin');
$_SESSION['admin'] = $admin;
session_register('home');
$_SESSION['home'] = $home;
session_register('aword');
$_SESSION['aword'] = $aword;
session_register('tracts');
$_SESSION['tracts'] = $tracts;
session_register('booklet');
$_SESSION['booklet'] = $booklet;
session_register('gospel');
$_SESSION['gospel'] = $gospel;
session_register('outlines');
$_SESSION['outlines'] = $outlines;
session_register('commentary');
$_SESSION['commentary'] = $commentary;
session_register('reviews');
$_SESSION['reviews'] = $reviews;
session_register('testimonies');
$_SESSION['testimonies'] = $testimonies;
session_register('contact');
$_SESSION['contact'] = $contact;
session_register('directions');
$_SESSION['directions'] = $directions;
session_register('serving');
$_SESSION['serving'] = $serving;
session_register('ecbts');
$_SESSION['ecbts'] = $ecbts;
session_register('tvshow');
$_SESSION['tvshow'] = $tvshow;
session_register('university');
$_SESSION['university'] = $university;
session_register('bible');
$_SESSION['bible'] = $bible;
session_register('recordings');
$_SESSION['recordings'] = $recordings;
session_register('phone');
$_SESSION['phone'] = $phone;
}
else {
$perror = "Please enter a valid Password!";
session_destroy();
}
}
else {
$nerror = "Please enter a valid Username!";
session_destroy();
}
}
}
if (isset($_SESSION['Name'])) {
$Name = $_SESSION['Name'];
$User_Id = $_SESSION['User_Id'];
$admin = $_SESSION['admin'];
$home = $_SESSION['home'];
$aword = $_SESSION['aword'];
$tracts = $_SESSION['tracts'];
$booklet = $_SESSION['booklet'];
$gospel = $_SESSION['gospel'];
$outlines = $_SESSION['outlines'];
$commentary = $_SESSION['commentary'];
$reviews = $_SESSION['reviews'];
$testimonies = $_SESSION['testimonies'];
$contact = $_SESSION['contact'];
$directions = $_SESSION['directions'];
$serving = $_SESSION['serving'];
$ecbts = $_SESSION['ecbts'];
$tvshow = $_SESSION['tvshow'];
$university = $_SESSION['university'];
$bible = $_SESSION['bible'];
$recordings = $_SESSION['recordings'];
$phone = $_SESSION['phone'];
}
?>
To explain all that, in basic, this is the administration side for church web site that has 10,000 plus documents (and growing) that are being made available online. I am attempting to setup access based upon the area that different people oversee. I.e. if you are responsible for outlines and booklets your user entry in the database will have a value of "1" set to the "outlines" and "booklet" fields and a value of "0" set for any other field. I have a separate menu file that is included in order to see a menu item it will first check to see if the session has that item set to a value of "1".
My problem is this, my code works great, but for some reason it will not log me out, technically. My code for determining what is shown looks like so:
if (!isset($Name)){ include('login.php'); }
if (isset($Name)) {
include('menu.php');
include('logout.php');
and when I click the Logout button (log out code above) it takes me back to just a login screen but if I then just click the login button without entering a username or password it shows all the menu items I have access permissions for (all 😃 ). Even if I close the browser window and open a new one it still will let me in without typing a username or password.
As you can see I have tried several things in an attempt to stop this, but they don't. I am guessing that some how the session is staying active but what is confusing from my understanding of sessions is that when I set the session_id() so that it will display for me each time, after closing and then opening a new browser window and click login, it was a new id number so I don't know how the browser is getting the the user information and displaying it. I would think that:
if ($userid != $User_Id) {
echo"tuff luck sucker no access";
}
or
if ($userpass != $User_Pass) {
echo"tuff luck sucker no access";
}
(not real code in the site but basically my code is setup that way)
Any help would be great.
Also, probably my code is a little awkward and bulky so any suggestions to make it more streamlined or dynamic would be welcome as this is the first time I have had to deal with user access permission and hence sessions.