0

Trying to make a login form become hidden once a successful login has happened within the form, so that the form cannot be seen by the user once they are logged in, but I still need a success message to be displayed when a successful login has happened. I have the form and the login sorted and it displayed a success message when a successful login has been made but I cant hide the form upon successful login. i have an if statement set up using isset but that's as far as I got I don't know what needs to go inside the if statement for me to hide the form.

This is the PHP code for my login page -

        <?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');
    ?>
8
  • @bromber it is probably wrong that was just as far as I had gotten before becoming stuck Commented Feb 23, 2021 at 13:37
  • @brombeer yes there should be brackets i have change this now Commented Feb 23, 2021 at 13:38
  • What doesn't work with your approach? Either use an opening <div> as you did (remember to close it after your form using the same if) or place your complete form inside the if so it doesn't get rendered at all. Commented Feb 23, 2021 at 13:38
  • the page doesn't load with what I have right now, would putting the entire div inside the if statement Commented Feb 23, 2021 at 13:39
  • Also take good care of naming stuff, you use $login_ok above but $login_okay in your if Commented Feb 23, 2021 at 13:40

1 Answer 1

1

There are a number of different ways you can approach this problem, but i think the best way to do this is with a session variable. Make your login function create a session variable that will follow the logged in user around throughout the website, then its just a matter of checking for the value and either displaying or hiding the form based on that. Here is a simplified example:

<?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 } ?>
Sign up to request clarification or add additional context in comments.

12 Comments

How would i do it without the session?
You need the session. How are you going to know that the user is logged in when they move to another page?
as this is for a university project I will be coming to that part soon, is it a necessity to have a session?
Alternatively you can setup some kind of global variable but i wouldnt recommend it.
I attempted your way with the session however it didn't work, where should I have put things within the code as I put my form in where you said and it didn't work
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.