表单操作不会调用登录功能

时间:2019-11-08 10:15:51

标签: php authentication

我有一个HTML登录表单,该表单的操作将发送到另一个PHP文件,该文件中应有一个用于将用户登录到该站点的功能。当提交表单导致该php文件时,它显示一个空白页面,就像它不会触发该函数一样。我在函数顶部放置了echo和die,但是仍然没有任何反应。

此外,当我在函数外部回显某些内容时,它还会显示我在echo中输入的内容,因此它会调用正确的文件,只是不会加载函数。任何帮助表示赞赏。这是我的代码。

login.php

<?php
    if ($_SERVER['REQUEST_METHOD'] == "POST") {
        if (isset($_POST["submit"])) {
            login_function();
        }
    }
?>
<!DOCTYPE HTML>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Welcome to Love Her Feet</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="/login_assets/css/style.css">
        <link href="https://fonts.googleapis.com/css?family=Raleway:300,400,500&display=swap" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css?family=Open+Sans:400,600&display=swap" rel="stylesheet">
        <link rel="stylesheet" href="/login_assets/css/media.css">
        <script src="/login_assets/js/jquery.min.js"></script>
        <script src="/login_assets/js/modernizr.custom.js"></script>
    </head>
    <body>
        <header class="clear hBlack">
            <div class="jLogo"><a href="/"><img src="/login_assets/images/logo.png" alt=""></a></div>
        </header>
        <div class="logArea clear">
            <form action="custom_functions.php" method="post" enctype="application/x-www-form-urlencoded">
                <div class="logbox">
                    <div class="box clear">
                        <h2>Members Area</h2>
                        <div class="logTypes">
                            <input type="text" name="username" class="logtextbox" placeholder="Username or email">
                            <span class="text-danger"><?php echo $username_error;  ?></span>
                            <input type="password" name="password" class="logtextbox" placeholder="Password"><br>
                            <span class="text-danger"><?php echo $password_error;  ?></span>
                            <!-- <input type="text" name="captcha" class="logtextbox" placeholder="Enter the code shown below"><br>
                            <img style="margin: 0 auto;" src="captcha.php">
                            <span class="text-danger"><?php echo $captcha_error;  ?></span> -->
                            <div style="text-align: center">Remember my login: <input name="remember" type="checkbox"></div>
                        </div>
                    </div>
                    <input type="submit" value="submit" class="logBtn" name="submit"/>
                </div>
            </form>
        </div>
        <footer class="clear">
            <p class="fNav"><a href="/">Home</a><span>|</span>
                <a href="/">Log Out</a>
            </p>
        </footer>
    </body>
</html>

custom_functions.php

<?php
    //echo "AAAAAAAAA";

    function validation($form_data)
    {
        $form_data = trim(stripcslashes(htmlspecialchars($form_data)) );
        return $form_data;
    }
    function login_function() {
        //echo "AAAAAAAAAA";
        //die('!');
        session_start();
        require 'connection.php';
        $username_error = "";
        $password_error = "";
        //$captcha_error = "";
        $v_username = $_POST['username'];
        $v_password = $_POST['password'];
        //$v_captcha = $_POST['captcha'];
        $username = validation($v_username);
        $password = validation($v_password);
        //$captcha = validation($v_captcha);
        $remember = isset($_POST['remember']);
        if (empty($username)) {
            $username_error = "<p>Please enter your username!</p>";
        }
        if (empty($password)) {
            $password_error = "<p>Please enter your password!</p>";
        }
        if (!empty($username) && !empty($password)) {
            $sql = "SELECT * FROM member_auth WHERE username = :username";
            //$sql = "SELECT * FROM member_auth LIMIT 1";
            $stmt = $pdo->prepare($sql);

            $stmt->bindValue(':username', $username);

            $stmt->execute();

            $user = $stmt->fetch(PDO::FETCH_ASSOC);

            $cryptpass = $user['cryptpass'];

            if($user === false){
                $username_error = "<p>User doesn't exist</p>";
            } elseif ($user) {
                $newPass = crypt($password, $cryptpass);
                if ($cryptpass == $newPass) {
                    $_SESSION['loggedin'] = TRUE;
                    $_SESSION['username'] = $username;
                    if ($remember == "on") {
                        setcookie("remember", $username, time()+3600);
                    }
                    header('Location: login_success.php');
                } else {
                    $password_error = "<p>Password is not correct!</p>";
                }
            }
        }
    }
?>

2 个答案:

答案 0 :(得分:2)

将您的状况从同一页面移至custom_functions.php

if ($_SERVER['REQUEST_METHOD'] == "POST"){
    if(isset($_POST["submit"])) {
        login_function();
    }
}

<form action="" method="post" enctype="application/x-www-form-urlencoded">

答案 1 :(得分:0)

您在登录页面中调用登录函数,并在custom.php页面中定义该函数,因此在此文件中找不到任何函数,将您的代码置于相同的函数中即可。

ServiceUnavailableRetryStrategy