如何根据用户、管理员更改变量

How to change a variable according to the user, administrator

我有一个用户 table 有一个名为 id_role 的列。默认情况下,为所有新用户分配 id_role3

任何需要更高级别权限的用户,我手动将它们更新为 2 for administrator1 for super_admin.

问题是id_role仍然值上次注册的值,我不明白为什么。

如果我不添加新用户id_role,它会一直是1,每个人都会登录superadmin,如果我添加新用户,id_role会是3 每个人(甚至是管理员)都将以基本用户身份登录。

这是我的基本代码:

<?php
session_start();

$Nom = $_POST["Nom"];
$mdp = $_POST["mdp"];
//$id_role = $_POST["id_role"];

try{
    $bdd = new PDO('mysql:host=localhost;dbname=azer', 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
} catch(Exception $e) {
    die("acces imlpossible");
}

$st = $bdd->query("SELECT * FROM membre WHERE Nom='".$Nom."'")->fetch();
$mangetesmorts = $bdd->query("SELECT * FROM membre WHERE id_role");

if (password_verify($mdp, $st['mdp'])) {
    $_SESSION['Nom'] = $Nom;
    $_SESSION['activite'] = $st['activite'];    //$_SESSION['id_role'] = $mangetesmorts['id_role'];
    //var_dump($_SESSION['id_role']);

    //print_r($id_role);
    while ($donne = $mangetesmorts->fetch()) {
        if ($_SESSION['activite'] =='cricket') {
            header("Location: cricket.php");
        } elseif ($_SESSION['activite'] == 'foot') {
            header("Location: foot.php");
        } elseif (($donne['id_role'] == 2)) {
            header("Location: admin.php");
        } elseif ($donne['id_role'] == 1) {
            header("Location: admin_super.php");
        } else {
            header("Location: index2.php");}
        }
   }

这就是我之后尝试的:

<?php
session_start();

$Nom = $_POST["Nom"];
$mdp = $_POST["mdp"];
//$id_role = $_POST["id_role"];

try{
    $bdd = new PDO('mysql:host=localhost;dbname=azer', 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
} catch (Exception $e) {
    die("acces imlpossible");
}

$st = $bdd->query("SELECT * FROM membre WHERE Nom='".$Nom."'")->fetch();
$mangetesmorts = $bdd->query("SELECT * FROM membre WHERE id_role");

if (password_verify($mdp, $st['mdp'])) {
    $_SESSION['Nom'] = $Nom;
    $_SESSION['activite'] = $st['activite'];
    //$_SESSION['id_role'] = $mangetesmorts['id_role'];
    //var_dump($_SESSION['id_role']);

    //print_r($id_role);
    while ($donne = $mangetesmorts->fetch()) {
        $_SESSION['id_role']=$donne['id_role'];
        if ($_SESSION['activite'] =='cricket') {
            header("Location: cricket.php");
        } elseif ($_SESSION['activite'] == 'foot') {
            header("Location: foot.php");
        } elseif ($_SESSION['id_role'] == 2) {
            header("Location: admin.php");
        } elseif ($_SESSION['id_role'] == 1) {
            header("Location: admin_super.php");
        } else {
            header("Location: index2.php");
        }
    }
}

您的条件重定向块基于第二个查询,该查询未引用已成功登录的用户。(如果该查询完全有效)。

换句话说,while ($donne = $mangetesmorts->fetch()) { 将从您的 table 中提取所有行,然后仅迭代第一行,因为重定向将结束脚本。

因为您的第一个 SELECT 查询正在为用户返回整行数据作为数组 $st 只需使用它来确定重定向。

if (in_array($st['activite'], ['cricket', 'foot'])) {
    header("Location: {$st['activite']}.php");
} elseif ($st['id_role'] == 2) {
    header("Location: admin.php");
} elseif ($st['id_role'] == 1) {
    header("Location: admin_super.php");
} else {
    header("Location: index2.php");
}

您可以声明任何您喜欢的 $_SESSION 元素,我已将它们排除在 clarity/consistency 的条件块之外。