PHP 登录代码不起作用,用户停留在登录页面 [英] PHP Login code doesn't work, user stays stuck on the login page

查看:37
本文介绍了PHP 登录代码不起作用,用户停留在登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 PHP 新手,无法找到以下代码为何不起作用的答案.这应该很容易,但我无法弄清楚.这段代码没有报错,phpAdmin SQL 控制台中的SQL 语句是正确的.我已经搜索过网络 &StackOverflow,但找不到好的答案.怎么了?所有用户(无论是否在数据库中)都会被忽略并卡在登录页面上.

connect_error){die("连接失败:" . $conn->connect_error);header('定位:login.php');}别的 {//检查db中是否存在超级管理员用户$sql = "SELECT count(*) FROM admins WHERE AdminLevel = 1";$result = mysqli_query($conn,$sql);//检查查询是否返回任何行if(mysql_num_rows(($result) > 0) {包括'welcome.php';}//检查密码和用户名是否匹配if(($username === $value1) && ($password === $value2)) {$_SESSION['登录'] = TRUE;echo "Hello ".$value1.", 您已登录!<br>";}//如果登录/用户名和密码错误,则发送用户错误消息别的 {echo "用户名或密码不正确
";包括登录.php";}//关闭数据库连接$conn->close();}?>

登录表单:

<html lang="zh-cn"><头><meta charset="utf-8"><title>管理员登录</title><脚本>//检查表单的函数函数 chkForm(){//确定用户登录表单中的元素数量var intFormLen = document.forms[0].elements.length;//循环遍历表单字段以查看已输入值for (var i = 0; i < intFormLen; i++) {if (document.forms[0].elements[i].value == "") {//如果登录字段为空,则向用户发送错误消息document.getElementById(document.forms[0].elements[i].name).innerHTML="必填字段";document.forms[0].elements[i].focus();返回假;}}//清除表单域函数 clearWarn(fieldName){document.getElementById(fieldName).innerHTML = "";返回真;}返回;}<身体><h2>管理员登录</h2>

<div class="formLayout"><form action="#" method="post" onsubmit="return chkForm();"><label for="login">登录:</label><input type="text"name="login" onchange="return clearWarn('fieldName')"><div id="login" style="color:red"></div><br><label for="password">密码:</label><input type="password" name="password" onchange="return clearWarn('fieldName')"><div id="password" style="color:red"></div><br><br><input type="submit" name="cmdSubmit" value="登录"></表单>

</html>

解决方案

你设置你的 form action="#" 并且不要在 JavaScript 中提交它.

正如 Jason 所指出的,chkForm() 永远不会返回 true,这也会阻止表单提交.

I am new to PHP and can't find answers as to why the following code doesn't work. This should be easy, but I can't figure this out. This code produces no errors, and the SQL statement is correct in the phpAdmin SQL console. I've searched web & StackOverflow, but can't find a good answer. What's wrong? ALL users (whether in the db or not) get ignored and stuck on login page.

<?php
session_start();

//create function to check login form for admin or other type of user.      
//Redirect the admin user to the welcome page.

function login()
    {
        //strip login and password using in-build htmlspecialchars function
        $value1 = htmlspecialchars($_POST['login']);
        $value2 = htmlspecialchars($_POST['password']);    

        //set variables for the db connection
        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "mydb";
        $loggedin = '';

        //Create new connection to db            
        $conn = new mysqli($servername, $username, $password, $dbname);

        //Check connection and handle any error
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
            header('Locatin: login.php');
        }
        else {               
            //check if super admin user exists in db      
            $sql = "SELECT count(*) FROM admins WHERE AdminLevel = 1";
            $result = mysqli_query($conn,$sql);

            //check to see if query returns any rows
            if(mysql_num_rows(($result) > 0) {
                include 'welcome.php';
            }

            //check if the password and username match
            if(($username === $value1) && ($password === $value2)) {
                $_SESSION['loggedin'] = TRUE;
                echo "Hello ".$value1.", you are logged in!<br>";
            }
            //send user error message if login/username and password wrong
            else {
                echo "Incorrect username or password<br>";
                include 'login.php';
            }

            //close the db connection               
            $conn->close();
        }
?>

Login Form:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Admin Login</title>
<script>

//function to check the form
function chkForm()
    {
        //determine the number of elements in the user login form
        var intFormLen = document.forms[0].elements.length;

        //loop through the form fields to see that a value has been input
        for (var i = 0; i < intFormLen; i++) {
            if (document.forms[0].elements[i].value == "") {
                //send user an error message if login field empty
                document.getElementById(document.forms[0].elements[i].name).innerHTML="Required Field";
                document.forms[0].elements[i].focus();                                
                return false;
            }
        }

        //clear the form fields
        function clearWarn(fieldName)
            {
                document.getElementById(fieldName).innerHTML = "";                  
                return true;
            }

        return;
    }   
</script>
</head>
<body>
<h2>Admin Login</h2>           
<div class="phpEcho">
    <div class="formLayout">
        <form action="#" method="post" onsubmit="return chkForm();">
            <label for="login">Login:</label>
            <input type="text"name="login" onchange="return clearWarn('fieldName')">
            <div id="login" style="color:red"></div><br>
            <label for="password">Password:</label>
            <input type="password" name="password" onchange="return clearWarn('fieldName')">
            <div id="password" style="color:red"></div><br><br>
            <input type="submit" name="cmdSubmit" value="Log in">
        </form>
    </div>
</div>
</body>
</html>

解决方案

You set your form action="#" and don't submit it in JavaScript.

As noted by Jason, chkForm() will never return true, which would also prevent the form from submitting.

这篇关于PHP 登录代码不起作用,用户停留在登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆