具有新登录模板的新站点中的PhP登录代码

时间:2018-01-20 13:10:58

标签: php html forms login

有人可以帮助我将旧的PHP登录代码实现到新的登录模板。 我之前尝试过,页面也已加载,但PHP代码没有工作。

旧登录代码



Error in t.test.default(temp$value[temp$X1.1 == "CTROL"],    temp$value[temp$X1.1 ==  : 
  not enough 'y' observations




所以这是我从互联网上获得的模板的HTML网站。 Full template



<?php
// Include config file
require_once 'config.php';
 
// Define variables and initialize with empty values
$username = $password = "";
$username_err = $password_err = "";
 
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
 
    // Check if username is empty
    if(empty(trim($_POST["username"]))){
        $username_err = 'Please enter username.';
    } else{
        $username = trim($_POST["username"]);
    }
    
    // Check if password is empty
    if(empty(trim($_POST['password']))){
        $password_err = 'Please enter your password.';
    } else{
        $password = trim($_POST['password']);
    }
    
    // Validate credentials
    if(empty($username_err) && empty($password_err)){
        // Prepare a select statement
        $sql = "SELECT username, password FROM users WHERE username = :username";
        
        if($stmt = $pdo->prepare($sql)){
            // Bind variables to the prepared statement as parameters
            $stmt->bindParam(':username', $param_username, PDO::PARAM_STR);
            
            // Set parameters
            $param_username = trim($_POST["username"]);
            
            // Attempt to execute the prepared statement
            if($stmt->execute()){
                // Check if username exists, if yes then verify password
                if($stmt->rowCount() == 1){
                    if($row = $stmt->fetch()){
                        $hashed_password = $row['password'];
                        if(password_verify($password, $hashed_password)){
                            /* Password is correct, so start a new session and
                            save the username to the session */
                            session_start();
                            $_SESSION['username'] = $username;      
                            header("location: welcome.php");
                        } else{
                            // Display an error message if password is not valid
                            $password_err = 'The password you entered was not valid.';
                        }
                    }
                } else{
                    // Display an error message if username doesn't exist
                    $username_err = 'No account found with that username.';
                }
            } else{
                echo "Oops! Something went wrong. Please try again later.";
            }
        }
        
        // Close statement
        unset($stmt);
    }
    
    // Close connection
    unset($pdo);
}
?>
 
<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <title>Login</title>
    <link rel="stylesheet" type="text/css" href="/css/style.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
</head>
<body>
    <div class="body"></div>
        <div class="grad"></div>
        <div class="header">
            <div>SWED
                <span>Login
                </span>
            </div>
        </div>
        <br>
        <div class="login">
        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
            <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>"></div>
                <input type="text" name="username" placeholder="username" class="form-control" value="<?php echo $username; ?>">
                <span class="help-block" style="color: red;"><?php echo $username_err; ?></span>
                
            <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>"></div>
                <input type="password" placeholder="password" name="password" class="form-control">
                <span class="help-block" style="color: red;"><?php echo $password_err; ?></span>
            
                <input type="submit" class="btn btn-primary" value="Submit">

            <p style="color: grey;">Don't have an account? <a href="register.php" style="text-decoration: none; color: white;">Sign up</a></p>
        </form>
    </div>
</body>
</html>
&#13;
&#13;
&#13;

我只是学习php,所以请原谅我,如果它应该是一个非常简单的错误。 对不起我的英语不好,我希望它不是太重要。 感谢您的帮助

1 个答案:

答案 0 :(得分:0)

表单元素必须具有动作属性,该属性具有到php页面的链接

像:

<form class="login100-form validate-form"  action="login.php"  method="POST">

并且输入元素的名称必须与$_POST['username'] & $_POST['password']相同 在您的HTML模板中输入的名称与php变量不匹配 它必须像

<input class="input100" type="text" name="username">
<input class="input100" type="password" name="password">