2013-08-17 47 views
0

我想設置一個非常基本的登錄,將根據用戶的「類型」,無論是成員,或官員去兩個頁面之一。我可以讓它爲官員工作,或者讓它爲成員工作,但是當我嘗試使用elseif時,它無法正常工作。我的「高級職員」用戶可以登錄,但他會被帶到會員的頁面。我的「成員」用戶根本無法登錄。謝謝你的幫助。elseif不能在用戶特定的登錄工作

這裏是我的代碼:

<?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.'; 
    } 
    } 

回答

0

你有||(或)而不是&&(和)

試試這個

if (isset($_SESSION['authenticated']) && $SESSION['authenticated'] == 'member'){ 
    header("Location: $members_redirect"); 
    exit; 
} 
elseif (isset($_SESSION['authenticated']) && $SESSION['authenticated'] == 'officer'){ 
    header("Location: $officers_redirect"); 
    exit; 
} 
+0

YES!這就是它。我已經把我的頭髮拉出了幾個小時。謝謝!!!! – Type540

0

你在你的$ _SESSION有失蹤下劃線變量ifelseif

if (isset($_SESSION['authenticated']) || $SESSION['authenticated'] == 'member')

變化:

if (isset($_SESSION['authenticated']) && $_SESSION['authenticated'] == 'member')

elseif (isset($_SESSION['authenticated']) && $_SESSION['authenticated'] == 'officer')

+0

感謝您捕獲該錯字。我修好了,但它仍然是一樣的。 – Type540

+0

你有沒有改變或者是和? – user2250892