I'm trying to setup a very basic login that will go to one of two pages depending on the "type" of user, either a member, or an officer. I can make it work for the officer, or make it work for the member but when I try to use elseif it doesn't work right. my "officer" user can login, but he gets taken to the member's page. My "member" user can't login at all. Thanks for any help.
Here is my code:
<?php
if (!file_exists($userlist) || !is_readable($userlist)) {
$error = 'There is a major jam up at the Login Center. Please try again later.';
} else {
// read the file into an array called $users
$users = file($userlist);
// loop through the array to process each line
for ($i = 0; $i < count($users); $i++) {
// separate each element and store in a temporary array
$tmp = explode(',', $users[$i]);
// check for a matching record
if ($tmp[0] == $username && $tmp[1] == $password && rtrim($tmp[2]) == 'member') {
$_SESSION['authenticated'] = 'member';
$_SESSION['start'] = time();
session_regenerate_id();}
elseif ($tmp[0] == $username && $tmp[1] == $password && rtrim($tmp[2])== 'officer') {
$_SESSION['authenticated'] = 'officer';
$_SESSION['start'] = time();
session_regenerate_id();
break;
}
}
// if the session variable has been set, redirect
if (isset($_SESSION['authenticated']) || $SESSION['authenticated'] == 'member'){
header("Location: $members_redirect");
exit;}
elseif (isset($_SESSION['authenticated']) || $SESSION['authenticated'] == 'officer'){
header("Location: $officers_redirect");
exit;
} else {
$error = 'Invalid username or password.';
}
}
You have ||
(or) instead of &&
(and)
Try this
if (isset($_SESSION['authenticated']) && $SESSION['authenticated'] == 'member'){
header("Location: $members_redirect");
exit;
}
elseif (isset($_SESSION['authenticated']) && $SESSION['authenticated'] == 'officer'){
header("Location: $officers_redirect");
exit;
}
You have a missing underscore in your $_SESSION variable in your if
and elseif
if (isset($_SESSION['authenticated']) || $SESSION['authenticated'] == 'member')
change to:
if (isset($_SESSION['authenticated']) && $_SESSION['authenticated'] == 'member')
elseif (isset($_SESSION['authenticated']) && $_SESSION['authenticated'] == 'officer')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.