Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何修复此登录页面是';在php/mysql中不工作?_Php_Mysql_Database_Login - Fatal编程技术网

如何修复此登录页面是';在php/mysql中不工作?

如何修复此登录页面是';在php/mysql中不工作?,php,mysql,database,login,Php,Mysql,Database,Login,我使用mysql和用户角色创建了一个登录页面,但登录页面不工作/未加载 代码如下: <?php include "config.php"; if (isset($_POST['username']) && isset($_POST['password'])){ $username = $_POST['username']; $password = md5($_POST['password']);

我使用mysql和用户角色创建了一个登录页面,但登录页面不工作/未加载

代码如下:

<?php
      include "config.php";
        if (isset($_POST['username']) && isset($_POST['password'])){
            $username = $_POST['username'];
            $password = md5($_POST['password']);
            $stmr = db->prepare("SELECT * FORM login WHERE username=? AND password=?");
            $stmt->bindParam(1, username);
            $stmt->bindParam(2, password);
            $stmt->execute();
            $row = $stmt->fetch();
            $user = $row['username'];
            $pass = $row['password'];
            $id = $row['id'];
            $type = $row['type];
        if ($username==$user && $pass==$password) {
            session_stat();
            $_SESSION['username'] = $user;
            $_SESSION['password'] = $pass;
            $_SESSION['id'] = $id;
            $_SESSION['type'] = $type;
            if($type=='Member'){
            ?>
            <script>window.location.href="index.php"</script>
            <?php
            } else {
                ?>
            <script>window.location.href="index.php"</script>
            <?php
            }
           } else {
              ?>                 
             <strong>Oops!</strong> You Can not Not Visit This Page.
             </div>
            <?php
           }
        }
      ?>
更改会话_stat();
到会话_start()


并确保在php.ini文件中显示错误,代码中有几个语法错误,如下所示:

  • $stmt->bindParam(1,用户名)
  • $stmt->bindParam(2,密码)
  • $type=$row['type]
  • session_stat()

  • 安全漏洞
    md5()
    尤其是在生产服务器中执行所有这些操作时,应使用php.net文档中提供的
    password\u hash()
    和password\u verify()

  • 我在代码中发现了更多的错误,最好在您仍在开发的时候在每页的顶部添加这一行,这样您可以在live server上看到所有错误,然后删除这一行。查看我的评论,了解更多错误

  • 这是你的登录页面应该没有错误的样子

    <?php
    ob_start();
    session_start();
    
    if (isset($_SESSION['username'])) {
    
        header("location:dashboard.php"); // if the user is logged in already dont log themm in again, its gonna irritate them.
    }
    include "config.php";
    
    if (isset($_POST['SubmitButtonName'])) {
    
        if (empty($_POST['username']) || empty($_POST['password'])) {
    
            //return error to the user
        } else {
    
            $username = $_POST['username'];
            $password = $_POST['password'];
    
            try {
    
                $stmt = $db->prepare("SELECT * FROM login where username = ? ");
                $stmt->execute([$username]);
                $results = $stmt->fetchall(PDO::FETCH_ASSOC);
    
                if (count($results) > 0) {
                    //username is correct
                    foreach ($results as $key => $row) {
                        //now verify password
    
                        if (password_verify($password, $row['password'])) {
                            // password is correct
                            $_SESSION['username'] = $row['username'];
                            $_SESSION['id']       = $row['id'];
    
                            if ($row['type'] == "Member") {
    
                                header("location:page.php");
                            } else {
    
                                //type not a member redirect to different page
                                header("location:AppropriatePage.php");
                            }
    
                        } else {
                            //password incorrect return proper message
    
                        }
    
                    }
    
                } else {
    
                    //user account does not exists return proper message
                }
            }
            catch (PDOException $ex) {
    
                error_log("Error : " . $ex->getMessage());
            }
    
        }
    
    }
    ?>
    
    
    
    NB:在您的注册页面上删除这一行
    $password=md5($\u POST['password'])
    并替换为
    $password=
    密码\u散列($\u POST['Password'],密码\u默认值)然后
    完成后重新注册,您可以再次登录。

    阅读Jay Blanchard关于php中正确密码准备的博客 在这里: 然后,对于pdo准备的声明,请阅读您的常识博客:


    问题在于许多打字错误和语法错误,这就是为什么你们的页面并没有加载。此处修复了打字错误和语法:

        include "config.php";
        if (isset($_POST['username']) && isset($_POST['password'])){
            $username = $_POST['username'];
            $password = md5($_POST['password']);
            $stmt = $db->prepare("SELECT * FROM login WHERE username=? AND password=?"); //changed
            $stmt->bindParam(1, $username); // changed
            $stmt->bindParam(2, $password); // changed
            $stmt->execute();
            $row = $stmt->fetch();
            $user = $row['username'];
            $pass = $row['password'];
            $id = $row['id'];
            $type = $row['type']; /// you forgot \' here
        if ($username==$user && $pass==$password) {
            session_start(); // changed
            $_SESSION['username'] = $user;
            $_SESSION['password'] = $pass;
            $_SESSION['id'] = $id;
            $_SESSION['type'] = $type;
            if($type=='Member'){
    

    是否出现错误?
    $row=$stmt->fetch()
    将获取一条记录此错误:mydomain.com页面不工作mydomain.com当前无法处理此请求。HTTP错误500Error在这一行:
    $type=$row['type]
    你错过了结束单引号请不要使用
    md5()
    不再安全使用
    password\u hash()
    password\u verify()
    他说他的页面没有加载。因为他写的课时你开始拼写错误了。如果没有设置显示错误,则不会显示任何错误。我知道,他犯了很多错误。我发现我对他说了什么错误请告诉我,什么错误?这不是答案-问题是为什么页面不工作,而不是写新脚本。顺便说一句,你有太多的if/else,我永远不会接受这个sphagetti代码。当你回答问题时,你不仅关注OP,还关注未来用户的同一个问题@VladdAnyhow,你的代码不是问题的答案,它写得很糟糕,永远不应该成为应该如何做到这一点的例子。你对所有试图让你的答案被接受的人都投了反对票,但仍然提供了糟糕的代码。我没有对任何人投反对票,我也没有试图让我的答案被接受。我只是提供了OP的解决方案,感谢你指出我的代码在将来写得很糟糕。我会尝试写一个更好的,但这不是解决方案。在那里,我可以向您展示您的代码,但这些缩进和sphagetties是正确编写和重构的?我试着把它贴在这里,但是不行,对一个评论家来说太长了。你在干什么?这不应该运行,而是替换部分操作代码。你为什么投票否决我??你是认真的吗?我还是不改变我的DV这个答案没什么好处