使用PHP if语句隐藏表单Div



试图使一个登录表单成为隐藏一旦成功登录发生在表单内,这样表单不能被用户看到一旦他们登录,但我仍然需要一个成功的消息,当成功登录发生时显示。我有表单和登录排序,它显示了一个成功的消息,当成功登录已作出,但我不能隐藏表单成功登录。我有一个if语句设置使用isset,但这是我得到的,我不知道什么需要去里面的if语句为我隐藏的形式。

这是我登录页面的PHP代码-

<?php
require('includes/application_top.php');
$page_title='Login!';
if (isset($_POST['action'])) {
$username_input = $_POST['username'];
$password_input = $_POST['password'];
$login_ok = login($username_input, $password_input);
}
require('includes/site_header.php');
?>
<style>
<?php
require('css/login.css');
?>
</style>
<br>
<br>
<br>
<?php
if (isset($login_ok)) {
?>
<div class="row">
<div class="col-sm-12">
<?php
if ( ! $login_ok) {
?>
<div class="alert alert-danger">
<p>Your credentials were invalid</p>
</div>
<?php
} else {
?>
<div class="alert alert-success">
<p>Login successful</p>
</div>
<?php
}
?>
</div>
</div>
<?php
}
?>
<!-- This is the if statement I mention -->
<?php 
if !isset($login_okay){
?>
<div class="wrapper dafswInDown" hidden>
}
?>

<div class="wrapper fadeInDown">
<div id="formContent">
<!-- Icon -->
<div class="fadeIn first">
<br>
<img src="images/Head1.jpg" style="height: 40%; width: 60%;" id="icon" alt="User Icon" />
<br>
<h3> Login! </h3>
</div>
<br>
<!-- Login Form -->
<form id="login_form" action="login_page.php" method="post">
<input type="text" class="fadeIn second" id="username" name="username" placeholder="Username" value="<?= isset($_POST['username']) ? htmlspecialchars($_POST['username']) : '' ?>" maxlength="100" />
<input type="password" class="form-control" id="password" name="password" placeholder="Password" value="" />
<input type="submit" class="fadeIn fourth" name="action" value="Login"/>
</form>
<!-- Remind Passowrd -->
<div id="formFooter">
<a class="underlineHover" href="#">Forgot Password?</a>
</div>

</div>
</div>

<?php
require('includes/application_bottom.php');
require('includes/site_footer.php');
?>

有许多不同的方法可以解决这个问题,但我认为最好的方法是使用会话变量。让你的登录功能创建一个会话变量,它将跟随登录用户在整个网站周围,然后它只是一个问题,检查值和显示或隐藏基于表单。下面是一个简单的例子:

<?php function login($username_input=false, $password_input=false){
// Do all your verification here
// If logged in then:
session_start();
$_SESSION["user"] = true;
}
// Then on the form side just do this:
if(!$_SESSION["user"]){ ?> 
FORM HERE
<?php } ?>

最新更新