根据值和会话显示警报

时间:2018-03-01 22:18:38

标签: php session

我想要完成的是,如果用户验证列为0,如果用户验证列为1,它将显示以下消息:

所以我让它工作但是如果他们关闭它我不希望它再次显示该会话。

$db = dbconnect();
    $stmt = $db->prepare("SELECT * FROM users WHERE ID = ?");
    $stmt->bind_param('s', $_SESSION['ID']);
    $stmt->execute();
    $result = $stmt->get_result();

    while ($row = mysqli_fetch_assoc($result)) {
        $id = $row['ID'];
        $verified = $row['Verified'];

      if ($verified == 0) {            

       echo '
        <div class="alert alert-warning alert-dismissible fade show" role="alert">
          Your account has not been verified.
          <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>';

            return true;        

        } else {

         return false;            
     }
  }

1 个答案:

答案 0 :(得分:1)

您应该设置一个会话变量,以根据从数据库中检索到的值显示警报,如下所示:

PHP

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['verifymsg']) {
        $_SESSION['VerifyMessage'] = false;
    }
}

$db = dbconnect();
$stmt = $db->prepare("SELECT * FROM users WHERE ID = ?");
$stmt->bind_param('s', $_SESSION['ID']);
$stmt->execute();
$result = $stmt->get_result();

while ($row = mysqli_fetch_assoc($result)) {
    $id = $row['ID'];

    if (!isset($_SESSION['VerifyMessage']) {
        $_SESSION['VerifyMessage'] = ($row['Verified'] == 0) ? true : false;
     }

  if ($_SESSION['VerifyMessage']) {            

   echo '
<form method="POST" action="yourscript.php">
    <input type="hidden" name="verifymsg" />
    <div class="alert alert-warning alert-dismissible fade show" role="alert">
      Your account has not been verified.
      <button type="button" class="close" data-dismiss="alert" aria-label="Close">
        <span aria-hidden="true">&times;</span>
      </button>
    </div>
</form>';

        return true;        

    } else {

     return false;            
  }
}

在这里,我使用ternary operation来设置一个名为'VerifyMessage'的会话变量(如果尚未设置)。我还在html中添加了名为<form>的{​​{1}}和隐藏输入。

当此脚本将verifymsg视为post变量时,它会将会话变量设置为verifymsg。这可以随心所欲地工作,例如使用ajax,但它显示了一个概念。