Php echo“您需要输入用户名”; $isValid=false; } 如果(空($email)){ 回显“您需要输入电子邮件地址”; $isValid=false; }elseif(!filter\u var($email,filter\u VALIDATE\u email)){ 回显“您需要有一个有效的电子邮件地址”; $isValid=false; } 如果($isValid){ $sql=“从用户名=?和密码=?以及电子邮件=?限制1”的帐户中选择*; $stmtselect=$db->prepare($sql); $result=$stmtselect->execute([$username、$password、$email]); $user=$stmtselect->fetch(PDO::fetch_ASSOC); 如果($stmtselect->rowCount()>0){ $\会话['accounts']=$user; echo“您已成功登录!”; }否则{ 回显“不正确的用户名、密码或电子邮件”; } }

Php echo“您需要输入用户名”; $isValid=false; } 如果(空($email)){ 回显“您需要输入电子邮件地址”; $isValid=false; }elseif(!filter\u var($email,filter\u VALIDATE\u email)){ 回显“您需要有一个有效的电子邮件地址”; $isValid=false; } 如果($isValid){ $sql=“从用户名=?和密码=?以及电子邮件=?限制1”的帐户中选择*; $stmtselect=$db->prepare($sql); $result=$stmtselect->execute([$username、$password、$email]); $user=$stmtselect->fetch(PDO::fetch_ASSOC); 如果($stmtselect->rowCount()>0){ $\会话['accounts']=$user; echo“您已成功登录!”; }否则{ 回显“不正确的用户名、密码或电子邮件”; } },php,if-statement,echo,Php,If Statement,Echo,编辑 如果未在上提供方法。但是,您的代码希望它是POST。将表单更改为 编辑 我不会使用任何HTML,相反,我只会制作一个非常简单的表单,将其发布到自身。这是您在使用任何web语言编程时学习的一项非常常见的第一项任务。这个页面本身忽略了您的数据库、样式和JS逻辑,应该可以100%自行工作。一旦证明可以开始使用会话和AJAX来增强它。但从简单开始 该表单不包括正常设置,如必需的或字段上的最佳类型,因为我只是尽量让它尽可能简单 我确实添加了errors数组,它不再需要$isValid,因为我们现在可

编辑

如果未在
上提供
方法
。但是,您的代码希望它是
POST
。将表单更改为

编辑

我不会使用任何HTML,相反,我只会制作一个非常简单的表单,将其发布到自身。这是您在使用任何web语言编程时学习的一项非常常见的第一项任务。这个页面本身忽略了您的数据库、样式和JS逻辑,应该可以100%自行工作。一旦证明可以开始使用会话和AJAX来增强它。但从简单开始

该表单不包括正常设置,如
必需的
或字段上的最佳类型,因为我只是尽量让它尽可能简单

我确实添加了errors数组,它不再需要
$isValid
,因为我们现在可以检查错误是否包含任何内容

请自己尝试这段代码,一旦你了解了它的工作原理,就开始修改它,如果你真的需要的话,可能会在这里提出新的问题



电子邮件
!stripos
不正确。它应该是
!==FALSE
这也不是验证电子邮件地址的方法您有很多嵌套的if/else,这些都不是必需的。我建议把所有这些都改成其他的,如果有的话instead@user3783243我试过了!==错误地说有错误,什么都不起作用。是的!正在显示stripos和我的消息。如果它等于“另一个改进点:在当前结构中,即使存在多个错误,您也只会向用户显示一个错误”,那么当我这样做时,它开始出错。这样,在更正错误并再次提交后,用户将看到下一个错误,然后重复该过程。。。作为一个用户,我希望表单能一次告诉我所有的错误。考虑发送错误消息数组。请不要用<代码> Error报告(0)进行开发;<代码>,您从一开始就在自找麻烦。在生产中,这是一个不同的故事,但在开发过程中从未发生过。当我这样做时,如果我输入了密码,而没有其他内容,它会说我需要输入密码。同样的问题。这里有一个在线的快速演示,显示正在设置密码,其他什么都没有。错误消息是“你需要输入用户名”:当我去那里时,如果我去掉了奶酪作为密码,它会说我需要一个密码而不是用户名或电子邮件,并且过滤器验证电子邮件不起作用,我已经更新了上面的示例,以包括多个单独验证四个项目的检查。这是一个在线测试版本,也显示了电子邮件部分的工作。如果您的电子邮件地址与该测试不兼容,请发布该电子邮件地址。SMTP地址有很多边缘情况,但在野外很少见。
<?php
error_reporting(-1);
session_start();
require_once('config.php');


$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];

$isValid = true;
if (empty($password)) {
    echo 'You need to enter a Password';
    $isValid = false;
}

if (empty($username)) {
    echo 'You need to enter a Username';
    $isValid = false;
}

if (empty($email)) {
    echo 'You need to enter a Email Address';
    $isValid = false;
}elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "You need to have a valid Email Address";
    $isValid = false;
}

if ($isValid) {
    $sql = "SELECT * FROM accounts WHERE username=? and password=? and email=? LIMIT 1";
    $stmtselect = $db->prepare($sql);
    $result = $stmtselect->execute([$username, $password, $email]);
    $user = $stmtselect->fetch(PDO::FETCH_ASSOC);
    if ($stmtselect->rowCount() > 0) {
        $_SESSION['accounts'] = $user;
        echo 'You have signed in successfully!';
    } else {
        echo 'Incorrect Username or Password or Email';
    }
}
<?php 
error_reporting(-1);
    session_start();
    
if(isset($_SESSION['hello_world_accounts'])){
    header("Location: index.php");
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Programming Knowledge Login</title>
    <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<div class="container h-100">
    <div class="d-flex justify-content-center h-100">
        <div class="user_card">
            <div class="d-flex justify-content-center">
                <div class="brand_logo_container">
                    <img src="img/logo.png" class="brand_logo" alt="Programming Knowledge logo">
                </div>
            </div>  
            <div class="d-flex justify-content-center form_container">
                <form method="post">
                    <div class="input-group mb-2">
                        <div class="input-group-append">
                            <span class="input-group-text"><em class="fas fa-user"></em></span>                 
                        </div>
                        <input type="text" name="username" id="username" class="form-control input_user" placeholder="Username" required>
                    </div>
                    <div class="input-group mb-2">
                        <div class="input-group-append">
                            <span class="input-group-text"><em class="fas fa-key"></em></span>                  
                        </div>
                        <input type="password" name="password" id="password" class="form-control input_pass" placeholder="Password" required>
                    </div>
                    <div class="input-group mb-1">
                        <div class="input-group-append">
                            <span class="input-group-text"><em class="fas fa-inbox"></em></span>                    
                        </div>
                        <input type="email" name="email" id="email" class="form-control input_pass" placeholder="Email" required>
                    </div>
                    <div class="form-group">
                        <div class="custom-control custom-checkbox">
                            <input type="checkbox" name="rememberme" class="custom-control-input" id="customControlInline">
                            <label class="custom-control-label" for="customControlInline">Remember me</label>
                        </div>
                    </div>
                
            </div>
            <div class="d-flex justify-content-center mt-1 login_container">
                <button type="button" name="button" id="login" class="btn login_btn">Login</button> 
            </div>
            </form>
            <div class="mt-3 mb-1">
                <div class="d-flex justify-content-center links">
                    Don't have an account? <a href="registration.php" class="ml-2">Sign Up</a>
                </div>
                <div class="d-flex justify-content-center">
                    <a href="#">Forgot your password?</a>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="http://code.jquery.com/jquery-3.3.1.min.js"
              integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
              crossorigin="anonymous"></script>
<script type="text/javascript" src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script>
    $(function(){
        $('#login').click(function(e){

            var valid = this.form.checkValidity();

            if(valid){
                var username = $('#username').val();
                var password = $('#password').val();
                var email = $('#email').val();
            }

            e.preventDefault();

            $.ajax({
                type: 'POST',
                url: 'jslogin.php',
                data:  {username: username, password: password, email: email},
                success: function(data){
                    alert(data);
                    if($.trim(data) === "1"){
                        setTimeout(' window.location.href =  "index.php"', 1000);
                    }
                },
                error: function(data){
                    alert('There were errors while doing the operation.');
                }
            });

        });
    });
</script>
</body>
</html>
<?php

error_reporting(-1);
$db_user = "root";
$db_pass = "";
$db_name = "hello_world_accounts";

$db = new PDO('mysql:host=localhost;dbname='. $db_name . ';charset=utf8', $db_user, $db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
<?php 

error_reporting(-1);
session_start();

    if(!isset($_SESSION['hello_world_accounts'])){
        header("Location: login.php");
    }

    if(isset($_GET['logout'])){
        session_destroy();
        unset($_SESSION);
        header("Location: login.php");
    }

?>
<!DOCTYPE>
<html lang="en">
<head>
<title>Welcome</title>
</head>
<body>
<p>Welcome to index</p>


<a href="index.php?logout=true">Logout</a>
</body>
</html>