简体   繁体   English

如果用户已登录,则登录页面无法重定向到另一个页面

[英]Login page cant redirect to another page if user has logged in

This is the code i have used to login. 这是我用来登录的代码。 If i give the wrong username and password i wont stay on the log in page and the index.php will automatically refresh? 如果我输入了错误的用户名和密码,我将不会停留在登录页面上,而index.php将自动刷新吗?

 <?php
    // When clicked on button Login
    if(isset($_POST['inloggen'])) {
        if (empty($_POST['username']) && empty($_POST['password'])) {

        } //Controls the statement if username and password are empty get an error else get into the database and get the login details.
        else {
            $username = $_POST['gebruikersnaam'];
            $password = $_POST['wachtwoord'];

            set_include_path (include "/database/DBconnectie.php");

            //de tabel selecteren van de database
            $sql = "SELECT gebruikersnaam, wachtwoord FROM klanten, medewerkers WHERE gebruikersnaam='$username' AND wachtwoord='$password'";
            $query = mysqli_query($conn, $sql);

            if ($query) {
                $row = mysqli_fetch_assoc($query);
                $dbgebruiker = $row['gebruikersnaam'];
                $dbwachtwoord = $row['wachtwoord'];
                $gebruikercheck = $row['med_gebruikersnaam'];
            }
            if ($username == $dbgebruiker && $password == $dbwachtwoord) {
                $_SESSION['gebruikersnaam'] = $dbgebruiker;
                $_SESSION['wachtwoord'] = $dbwachtwoord;
                $_SESSION['med_gebruikersnaam'] = $gebruikercheck;


                //controleren of de klant een medewerker is
                if ($gebruikercheck == $username) {
                    header('Location: medewerkers/index.php');
                } //als het geen medewerker is dan log je in als klant
                else {
                    header('Location: kopers/index.php');
                }
            } else {
                $melding = "Onjuiste gebruikersnaam en/of wachtwoord";

            }
            $conn->close();
        }
    }
    ?>

This is the tabel i've used to log on. 这是我用来登录的表格。 It is just a simple log in pup window. 这只是在pup窗口中的简单登录。

<!-- Gebruikersnaam & Wachtwoord Login form -->
                    <div class="user_login">
                        <form method="POST">
                            <label style="margin-top:-4%;">Gebruikersnaam:</label>
                            <input type="text" name="gebruikersnaam"/>
                            <br/>
                            <label style="margin-top:-6%;">Wachtwoord:</label>
                            <input type="password" name="wachtwoord"/>
                            <a href="#" class="forgot_password" style=" margin-top:-0.3%;">Wachtwoord vergeten?</a>
                            <br/>
                            <div class="action_btns" style="margin-top:-8%; margin-bottom:9%;">
                                <div class="one_half"><a href="#" class="btn back_btn"><i
                                            class="fa fa-angle-double-left"></i>Terug</a></div>
                                <div class="one_half last"><button type="submit" class="btn btn_red" name="inloggen">Inloggen</button></div>
                            </div>
                        </form>
                        <?=$melding;?>
                    </div>

Change this 改变这个

$sql = "SELECT gebruikersnaam, wachtwoord FROM klanten, medewerkers WHERE gebruikersnaam='$username' AND wachtwoord='$password'";

to

$sql = "SELECT med_gebruikersnaam,gebruikersnaam, wachtwoord FROM klanten, medewerkers WHERE gebruikersnaam='$username' AND wachtwoord='$password'";

Because you are not selecting med_gebruikersnaam , so: 因为您没有选择med_gebruikersnaam ,所以:

$gebruikercheck = $row['med_gebruikersnaam']; //will always be unidentified.

And if $gebruikercheck is unidentified, then this: 如果$gebruikercheck不明,则可以这样:

if ($gebruikercheck == $username) //will return false always.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM