当我将数据输入表单时,它不会重定向到success.php

问题描述 投票:-3回答:1

当我将数据输入表单时,它不会重定向到success.php,我似乎看不到我在做什么错。重定向应该在验证用户名和密码后发生,在这种情况下为$ username&$ password。如果我还要设置一个PHP函数来使用数据库中的登录详细信息验证登录凭据,我将如何做呢?

<!DOCTYPE html>
<html>
<head>
    <title>Bookstore</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="view/stylesheet/loginstyle.css">
</head>
<body>
    <div class="popup">
        <div class="access">
            <form action="controller/processlogin.php" method="post">
                <legend>Sign In</legend>
                    <input class="form-control" type="text" name="username" placeholder="Username">
                    <input class="form-control" type="password" name="password" placeholder="Password">
                    <input class="btn btn-primary mb-2" type="submit" name="">
            </form>
        </div>
    </div>
</body>
</html>

<?php
    session_start();
    //Check if session varible is true, if session varible is true it will redirect to specified page//
    $password = "1";
    $username = "2";

    if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true) {
        header("Location: success.php");
    }

    if (isset($_POST['username']) && isset($_POST['password'])) {
        if ($_POST['username'] == $username && $_POST['password'] == $password) {
            $_SESSION['logged_in'] = true;
            header("Location: success.php");
        }
    }
?>
php post webforms
1个回答
0
投票

在您的代码中发现一个非常严重的错误,

您的表单操作具有页面网址,因此来自表单的所有请求都将转到该页面。我从表单动作中删除了controller/processlogin.php的动作网址,

只需尝试一下。

<!DOCTYPE html>
<html>
<head>
    <title>Bookstore</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="view/stylesheet/loginstyle.css">
</head>
<body>
    <div class="popup">
        <div class="access">
            <form action="" method="post">
                <legend>Sign In</legend>
                    <input class="form-control" type="text" name="username" placeholder="Username">
                    <input class="form-control" type="password" name="password" placeholder="Password">
                    <input class="btn btn-primary mb-2" type="submit" name="">
            </form>
        </div>
    </div>
</body>
</html>

<?php
    session_start();
    //Check if session varible is true, if session varible is true it will redirect to specified page//
    $password = "1";
    $username = "2";

    if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true) {
        header("Location: success.php");
    }

    if (isset($_POST['username']) && isset($_POST['password'])) {
        if ($_POST['username'] == $username && $_POST['password'] == $password) {
            $_SESSION['logged_in'] = true;
            header("Location: success.php");
        }
    }
?>
© www.soinside.com 2019 - 2024. All rights reserved.