在“$ _SESSION”中正确地将userlvl存储在mysql DB中以便调用它

时间:2016-11-06 16:05:15

标签: php

我正在检查我的mysql数据库中的用户级别,如果匹配则显示链接。

这是我目前的代码:

<?php

$mysqli = new mysqli('host', 'name', 'psw', 'db');
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
if ($result = $mysqli->query("SELECT user_level FROM users WHERE user_level = 1 ")) {

    $row_cnt = $result->num_rows;

    printf("Result set has %d rows.\n", $row_cnt);
    }
    /* until here everything works fine - the code below seems to be not working */

if ($result == $_SESSION['userlevel']) {

    $link = 'AdminLayout.php';
    printf ('<a href="' .$link. '">Adminpanel </a>');
    /* close result set */
    $result->close();


}

/* close connection */
$mysqli->close();
?>

printf("Result set has %d rows.\n", $row_cnt);仅用于检查连接是否正常,并且检测到user_level = 1。

我将$_SESSION['userlevel']存储在我的登录表单中:

if(isset($_POST['submitted']))
{ 
session_start();

$mysqli = new mysqli('host', 'name', 'psw', 'db');


 if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
 else ($result = $mysqli->query("SELECT user_level FROM users WHERE user_level = 1 ")) {
 if (mysqli_num_rows($result == 1) {
$ulevel = $mysqli_fetch_array($result);
  $ulevel = $_SESSION['userlevel'];
  }
}
}
?>

不幸的是,我的登录表格不再适用于添加这些行。

有人可以帮助我让它发挥作用吗?如何正确存储登录帐户的用户级别,以便在另一个站点上调用它?

长话短说:我想检查我的MySQL数据库中的用户级别,如果已登录的用户匹配,则显示该链接。

1 个答案:

答案 0 :(得分:-1)

请更正您的代码,如下所示。 登录页面:

operator

和其他代码:

if(isset($_POST['submitted']))
{ 
session_start();

$mysqli = new mysqli('host', 'name', 'psw', 'db');


 if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
 elseif ($result = $mysqli->query("SELECT user_level FROM users WHERE user_level = 1 ")) {
 if (mysqli_num_rows($result) == 1) {
$_SESSION['userlevel'] = $mysqli_fetch_array($result);
  }
}
}
?>