PHP - 如何在索引页上打印登录表单上的错误?

标签 php html mysql forms authentication

好吧,这个问题有点难以言喻,但我会尽力而为。所以我目前有一个带有弹出登录表单的index.php 页面,并且我有带有所有登录php 内容的login.php 来处理请求。目前,我似乎只能在 login.php 页面上打印登录错误,这会打开一个空白的白色页面,上面只有错误,因为我的 index.php 表单上的表单操作设置为“我想要的是在我的index.php中的登录表单上打印错误,但让login.php处理php的其余部分。我希望这是有道理的。

<form action="login.php" method="post">
                <label>Email Address</label>
                <input type="text" name="email"/>
                <br />

                <label>Password</label>
                <input type="password" name="password"/>
                <br />
                <!--<?php 
                    if(empty($errors)===false){
                        echo output_errors($errors);    
                    }
                ?>-->
                <div class="checkbox">
                    <input id="remember" type="checkbox" />
                    <label for="remember">Keep me signed in</label>
                </div>

                <div class="action_btns">
                    <div class="one_half last"><input type="submit" class="btn btn-blue" value="Login"></div>
                    <div class="one_half last"><a href="#" id="register_form" class="btn">Sign up</a></div>
                </div>
            </form>

以上是我在index.php 页面上的表单的代码。注释掉的 php 就是我想打印错误的地方。 下面是我的login.php

    <?php
include 'init.php';


function sanitize($data){
    return mysql_real_escape_string($data);
}

function output_errors($errors){
    return '<ul><li>'.implode('</li><li>', $errors).'</li></ul>';
}

//check if user exists
function user_exists($email){
        $email = sanitize($email);
        return (mysql_result(mysql_query("SELECT COUNT(ID) FROM register WHERE email = '$email'"),0) == 1)? true : false;
}

//check if user has activated account
function user_activate($email){
        $email = sanitize($email);
        return (mysql_result(mysql_query("SELECT COUNT(ID) FROM register WHERE email = '$email' AND active =1"),0) == 1)? true : false;
}
function user_id_from_email($email){
    $email = sanitize($email);
    return (mysql_result(mysql_query("SELECT id FROM register WHERE email = '$email'"),0,'id'));
}
function login($email,$password){
    $user_id = user_id_from_email($email);
    $email = sanitize($email);
    $password = md5($password);

    return (mysql_result(mysql_query("SELECT COUNT(id) FROM register WHERE email = '$email' AND password ='$password'"),0) == 1)? $user_id : false;
}


if(empty($_POST)=== false){
    $email = $_POST['email'];   
    $password = $_POST['password'];
}

if(empty($email)|| empty($password) === true){
        $errors[] = "You must enter a username and a password"; 
}
else if(user_exists($email) === false){
    $errors[] = "Email address is not registered";  
}
else if(user_activate($email) === false){
    $errors[] = "You haven't activated your account yet";   
}
else{
    $login = login($email, $password);
    if($login === false){
        $errors[] = "email/password are incorrect";
    } else {
        $_SESSION['user_id'] = $login;
        header('Location: index.php');
        exit();
    }
}
if(empty($errors)===false){
    header('Location: index.php');
    echo output_errors($errors);    
}

?>

这也是我下面的 init.php,它存储数组。

    <?php
session_start();
error_reporting();
require 'connection.php';

$errors = array();

?>

最佳答案

开始之前的一个提示是永远不要将不常量的变量放入准备好的语句中,您允许用户直接输入 SQL 语句。

但更重要的是,如果您想接收错误,我建议您使用基本的 die($db->e​​rror); 来通过数据库返回错误.

始终记住在使用连接结束时$db->close();

关于PHP - 如何在索引页上打印登录表单上的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35272270/

相关文章:

php - 如何使用 Laravel 发送电子邮件?

php - ZipArchive::close(): 读取错误: 是一个目录

html - 如何在选择下拉列表中应用占位符以及 Angular 6 中的 [ngmodel]?

javascript - 每次插入数据库时​​自动递增 `trackingcode` 列

php - Laravel 查询生成器 : Unexpected output

php 和服务器端 javascript

javascript - 我可以用js生成SVG吗?

php - Javascript window.location.href 重定向不起作用

mysql - 为什么要限制 MySQL 中的列长度?

php - 从 PDO 准备好的语句中获取原始 SQL 查询字符串