2017-09-24 48 views
0

我希望能夠從一行中讀取賬戶類型,並以$session的名稱返回。假設我有一個名爲「accounts」的表格,其中有兩列用於用戶名和帳戶類型。當用戶登錄時,如果成功,它將啓動會話。這是當前的代碼:會話PHP的退貨行

if($count == 1 && $row['userPass']==$password && $row['type']=="Advanced") { 
    $_SESSION['userAdvanced'] = $row['userId']; 
    header("Location: index.php"); 

但是我想使它這樣我就可以有這樣的事情:

if($count == 1 && $row['userPass']==$password) { 
    $_SESSION['user'+[type]] = $row['userId']; 
    header("Location: index.php"); 

,這樣它會返回「userAdvanced」。

我還想在一個if語句中可以有多個$session類型。我試過這個,但它不起作用:(而不是將兩個單獨的if語句合併成一個)。

<?php if(isset($_SESSION['userBasic'],['userAdvanced'])){ ?> 
    <a class="link" href="/index.php?logout" style="text-decoration:none">Logout</a> 

道歉,如果這沒有多大意義,請讓我知道該怎麼做才能改善問題。謝謝。

回答

0

對於第一個問題,你可能只是這樣做

if($count == 1 && $row['userPass']==$password) { 
    $_SESSION['user' . $row['type']] = $row['userId']; 
    header("Location: index.php"); 
} 

而對於第二個,你可以定義一個函數來爲你做的。

function checkUserType(array $types) { 
    foreach ($types as $type) { 
     if (isset($_SESSION('user' . $type))) { 
      return true; 
     } 
    } 
    return false; 
} 

<?php if(checkUserType(['Basic', 'Advanced'])): ?> 
    <a class="link" href="/index.php?logout" style="text-decoration:none">Logout</a> 
<?php endif ?> 
+0

Awesome stuff thanks @LeoAso – noone