2017-09-24 47 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