页面从表单重定向到我的PHP脚本 [英] Page is redirecting to my php script from form

查看:62
本文介绍了页面从表单重定向到我的PHP脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make a login system with php but i cant figure out why my form is just redirecting me to the script file when i click on my login button. it works fine on my sign up form. everything seems to work exept its redirecting me to the script file. My php file that contains my form: https://codepen.io/hubbe-andersson/pen/yGOPoM

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

I have put my phpscript into my header.php instead and now im getting ERR_TOO_MANY_REDIRECTS in chrome what is caussing this?

<?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();
    }

推荐答案

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

line 6 error: use [...] instead of {...}

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

Look at the middle code block. Everything inside that focuses on determining which error the results will return, so establish that in your $_SESSION data and then redirect header("Location.... once. Most mistakes are little ones like the line 6 error, make a habit of adding // comments to your code so that it's easier to sort out the lines and sections of your code.

<?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传递的查询字符串,以防止人们尝试在没有相关表格的情况下访问该页面.

Your index.php page need to start off by looking for the query string passed via $_SESSIONS to prevent people from trying to access the page without the related form.

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

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

Ask your teacher to explain finding errors and solving problems so you can learn what to look for.

这篇关于页面从表单重定向到我的PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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