PHP-当语句为真时隐藏元素

时间:2018-06-20 17:15:20

标签: php hide elements

我有这段代码来隐藏元素,但是第一部分是隐藏,第二部分总是显示,无论我是像用户还是管理员登录都无关紧要。我不明白那里出了什么问题...

我希望当我像用户一样登录时显示第一个下拉菜单内容,而当我像管理员一样登录时显示第二个下拉菜单内容,但是当我像用户或管理员那样登录时仅显示第二个下拉菜单内容。

代码:

<?php
if($parola == $data['parola'] && $data['admin'] == '0'){?>
<div class="dropdown">
  <button class="dropbtn"><?php echo $_SESSION['mail']; ?></button>
  <div class="dropdown-content">
    <a href="adauga.php">Adauga anunt</a>
    <a href="anunturi.php">Anunturile mele</a>
    <a href="edit.php">Editeaza profil</a>
	<a href="logout.php">Delogeaza-te</a>
  </div>
</div>	
<?php } else { ?>		
<div class="dropdown">
  <button class="dropbtn"><?php echo $_SESSION['mail']; ?></button>
  <div class="dropdown-content">
    <a href="/admin/index.php">Control Panel</a>
	<a href="logout.php">Delogeaza-te</a>
  </div>
</div>	
<?php } ?>

用户,管理员和被禁用户的登录名。

<?php
	require 'config.php';
	if(isset($_POST['login'])) {
		$errMsg = '';
		// Get data from FORM
		$mail = $_POST['mail'];
		$parola = $_POST['parola'];
		if($mail == '')
			$errMsg = 'Introduceti adresa de mail.';
		if($parola == '')
			$errMsg = 'Introduceti parola.';
		if($errMsg == '') {
			try {
				$stmt = $connect->prepare('SELECT mail, parola, admin, banned FROM acc_table WHERE mail = :mail');		
				$stmt->execute(array(
					':mail' => $mail
					));
				$data = $stmt->fetch(PDO::FETCH_ASSOC);
				if($data == false){
					$errMsg = "Contul asociat cu mail-ul $mail nu a fost gasit.";
				}
				else {
					if($parola == $data['parola'] and $data['admin'] == '1') {
						$_SESSION['mail'] = $data['mail'];
						$_SESSION['parola'] = $data['parola'];
						$_SESSION['admin'] = $data['admin'];
						header('Location: ../admin/index.php');
						exit;
					}
					else
						$errMsg = 'Parola nu este buna.';
				}
				
					if($parola == $data['parola'] and $data['banned'] == '1') {
						$_SESSION['mail'] = $data['mail'];
						$_SESSION['parola'] = $data['parola'];
						$_SESSION['banned'] = $data['banned'];
						header('Location: ban.html');
						exit;
					}
					else
						$errMsg = 'Parola nu este buna.';
					
					if($parola == $data['parola'] and $data['admin'] == '0') {
						$_SESSION['mail'] = $data['mail'];
						$_SESSION['parola'] = $data['parola'];
						$_SESSION['admin'] = $data['admin'];
						header('Location: profile.php');
						exit;
					}
					else
						$errMsg = 'Parola nu este buna.';
									
			}
			catch(PDOException $e) {
				$errMsg = $e->getMessage();
			}
		}
	}

3 个答案:

答案 0 :(得分:0)

尝试以下代码,看看它是否适合您。

如果这不起作用,请告诉我您遇到什么错误。

<?php
require 'config.php';

if (isset($_POST['login'])) {

    $errMsg = '';
    // Get data from FORM
    $mail = $_POST['mail'];
    $parola = $_POST['parola'];
    if ($mail == '') {
        $errMsg = 'Introduceti adresa de mail.';
    } else if ($parola == '') {
        $errMsg = 'Introduceti parola.';
    } else {
        if ($errMsg == '') {
            try {
                $stmt = $connect->prepare("SELECT mail, parola, admin, banned FROM acc_table WHERE mail = :mail");
                $stmt->bindParam(':mail', $var_mail);
                $var_mail = $_POST['mail'];
                $stmt->execute();
                if ($data = $stmt->fetch()) {
                    if ($parola == $data['parola'] and $data['admin'] == '0') {
                        $_SESSION['mail'] = $data['mail'];
                        $_SESSION['parola'] = $data['parola'];
                        $_SESSION['admin'] = $data['admin'];
                        header('Location: profile.php');
                        exit;
                    } else if ($parola == $data['parola'] && $data['admin'] == '1') {
                        $_SESSION['mail'] = $data['mail'];
                        $_SESSION['parola'] = $data['parola'];
                        $_SESSION['admin'] = $data['admin'];
                        header('Location: ../admin/index.php');
                        exit;
                    } else if ($parola == $data['parola'] && $data['banned'] == '1') {
                        $_SESSION['mail'] = $data['mail'];
                        $_SESSION['parola'] = $data['parola'];
                        $_SESSION['banned'] = $data['banned'];
                        header('Location: ban.html');
                        exit;
                    } else {
                        $errMsg = 'Parola nu este buna.';
                    }
                } else {
                    $errMsg = "Contul asociat cu mail-ul ".$var_mail." nu a fost gasit.";
                }
            }
            catch(PDOException $e) {
                $errMsg = $e->getMessage();
            }
        }
    }
}

答案 1 :(得分:0)

放入这样的if条件:-

if($data['admin'] === '0'){
    //The rest code runs here
}

让我知道是否可行。

答案 2 :(得分:0)

我可以解决我的问题。

他不使用标头重定向,但是使用JavaScript。 我曾经强迫用户停留在页面上,

<?php
	if(['admin'] == '1')?>
		<script>
		window.location = 'admin/index.php';
		</script>

感谢大家的帮助,以帮助我解决这个问题。