页面从表单重定向到我的PHP脚本

时间:2018-12-15 07:24:46

标签: php database mysqli

我正在尝试使用php创建登录系统,但是当我单击登录按钮时,我无法弄清为什么我的表单只是将我重定向到脚本文件。在我的注册表单上可以正常使用。似乎一切正常,将我重定向到脚本文件。我的php文件包含以下格式:https://codepen.io/hubbe-andersson/pen/yGOPoM

编辑:我将phpscript放到header.php中,现在我在chrome中获取ERR_TOO_MANY_REDIRECTS是什么原因呢?

<?php
    if(isset($_POST['login-sub'])) {
        require 'databash.php';

        $username = $_POST['mailname'];
        $password = $_POST{'pwd'};

        if(empty($username) || empty($password)) {
            header("Location: index.php?error=tommarutor");
            exit();
        } else {

            $sql = "SELECT * FROM USERS WHERE uidUsers=? OR emailUsers=?";
            $stmt = mysqli_stmt_init($conn);
            if(!mysqli_stmt_prepare($stmt, $sql)) {
                header("Location: index.php?error=sqlerror");
                exit();
            } else {

                mysqli_stmt_bind_parem($stmt, "ss", $username, $username);
                mysqli_stmt_execute($stmt);
                $resultat = mysqli_stmt_get_result($stmt);
                if ($row = mysqli_fetch_assoc($resultat)) {
                    $checkpwd = password_verify($password, $row['pwdUsers']);
                    if($checkpwd == false) {
                        header("Location: index.php?error=fellosenord");
                        exit();
                    }
                    else if ($checkpwd == true) {
                        seassion_start();
                        $_SESSION['userId'] = $row['idUsers'];
                        $_SESSION['userUid'] = $row['uidUsers'];
                        header("Location: index.php?login=lyckades");
                        exit();

                    }
                    else {
                        header("Location: index.php?error=fellosenord");
                        exit();
                    }
                }
                else {
                    header("Location: index.php?error=ingenanvandare");
                    exit();
                }
            }
        }
    } else {
       header("Location: index.php");
       exit();
    }

1 个答案:

答案 0 :(得分:0)

第6行错误:使用[...]代替{...}

查看中间代码块。内部的所有内容都集中在确定结果将返回哪个错误,因此请在您的$_SESSION数据中确定该错误,然后将header("Location....重定向一次。大多数错误都是类似于第6行错误的小错误,养成在代码中添加// comments的习惯,以便更轻松地整理代码的行和部分。

<?php
    if(isset($_POST['login-sub'])) {   
        require 'databash.php';

        $username = $_POST['mailname'];
        $password = $_POST['pwd'];

        if(empty($username) || empty($password)) {    
            header("Location: index.php?error=tommarutor");
            exit();
        } else {      

            $sql = "SELECT * FROM USERS WHERE uidUsers=? OR emailUsers=?";
            $stmt = mysqli_stmt_init($conn);
            if(!mysqli_stmt_prepare($stmt, $sql)) {    
                header("Location: index.php?error=sqlerror");
                exit();
            } else {        

                mysqli_stmt_bind_parem($stmt, "ss", $username, $username);
                mysqli_stmt_execute($stmt);
                $resultat = mysqli_stmt_get_result($stmt);

                if ($row = mysqli_fetch_assoc($resultat)) {
                    $checkpwd = password_verify($password,  $row['pwdUsers']);
                    if($checkpwd == false) {
                        session_start();
                        $_SESSION['error']="fellosenord";
                    }  else if ($checkpwd == true) {
                        seassion_start();
                        $_SESSION['userId'] = $row['idUsers'];
                        $_SESSION['userUid'] = $row['uidUsers'];
                        $_SESSION['login']="lyckades");
                    } else {
                        session_start();
                        $_SESSION['error']="fellosenord";
                    }   //end checkpwd
                } else {    //  if fetch fails, send error
                    session_start();
                    $_SESSION['error']="ingenanvandare";
                }   //  end fetch
                header("Location: index.php");

            }   // end if stmt exists
        }   // end not empty input
    } else { // if isset isn't set...
       header("Location: index.php");
       exit();
    } //   end isset
php database mysqli

您的index.php页面需要首先查找通过$_SESSIONS传递的查询字符串,以防止人们尝试在没有相关表格的情况下访问该页面。

session_start();
if ( empty($_SESSION['id']) ) {   // no session info...
...
} else {
...
}

请您的老师解释发现错误和解决问题的方法,以便您了解要查找的内容。