如何使此登录表单动态重定向?

时间:2015-01-09 10:53:38

标签: php mysql login passwords

我有一个简单的登录表单,我只想重定向到' login.php'如果一切正确的话。我怎样才能做到这一点?目前它会将您重定向到“login.php”#39;每次按下提交按钮,即使我已经使用PHP进行表单验证。

***所有HTML都已正确设置,我觉得没有必要添加它。

functions.php:

function db_connect() {

    define("DB_SERVER", "localhost");
    define("DB_USER", "user");
    define("DB_PASS", "password");
    define("DB_NAME", "database");

    $connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);

    mysqli_select_db("database");

}

文件开头:

<?php

    session_start();    // Starts the session for login
    require_once 'functions.php'; // This has the functions for the login process

?>

文件中间:

<?php

        $form = "<form action='login.php' method='post'>
                    <table>

                        <tr>
                            <td>Username:</td>
                            <td><input type='text' name='user' /></td>
                        </tr>
                        <tr>
                            <td>Password:</td>
                            <td><input type='password' name='password' /></td>
                        </tr>
                        <tr>
                            <td></td>
                            <td><input type='submit' name='login_btn' value='Login' /></td>
                        </tr>

                    </table>
                </form>";

        if ($_POST['login_btn']) {

            $user = $_POST['user'];
            $password = $_POST['password'];

            if ($user) {

                if ($password) {

                    db_connect();

                        $password = md5(md5("dF83sDFJ9f8" . $password . "ugj483DAhD2"));

                        $query = mysqli_query("SELECT * FROM user WHERE username='" . $user . "'");
                        $numrows = mysqli_num_rows($query);
                        // Checks to see which form rows have data

                        // If the username is entered (if there is 1 piece of data in $numrows)
                        if ($numrows == 1) {
                            $row = mysqli_fetch_assoc($query);
                            $db_id = $row['id'];
                            $db_user = $row['username'];
                            $db_pass = $row['password'];
                            $db_active = $row['active'];

                            if ($password == $db_pass) {

                                if ($db_active == 1) {

                                    $_SESSION['userid'] = $db_id;
                                    $_SESSION['username'] = $db_user;

                                    echo "You have been logged in as <b>" . $db_user . "</b>";

                                } else {
                                    echo "You must activate your account to login " . $form;
                                }

                            } else {
                                echo "Incorrect password " . $form;
                            }

                        } else {
                            echo "Incorrect username " . $form;
                        }

                    mysqli_close();

                } else {
                    echo "You must enter your password " . $form;
                }

            } else {
                echo "You must enter your username " . $form;
            }

        } else {

            echo $form;

        }

    ?>

1 个答案:

答案 0 :(得分:0)

我不明白。由于action属性,您的表单将自动提交到login.php。如果您不想使用javascript进行表单验证,请将表单操作属性更改为$_SERVER['PHP_SELF']。它将为自己提交数据,当一切正常时,您可以给它header("Location: login.php");echo<meta http-equiv="refresh" content="0; url=login.php">。我希望它有所帮助。