dsaff82024 2016-05-06 01:03
浏览 44
已采纳

PHP登录代码不起作用,用户仍然卡在登录页面上

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>
  • 写回答

2条回答 默认 最新

  • dqwh1203 2016-05-06 04:54
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据