繁体   English   中英

登录页面不直接-显示空白页面PHP

[英]Login Page doesnt direct - shows a blank page PHP

所以我有一个登录脚本,不太确定为什么它不起作用。 在我的用户表中,我有以下字段:

表:用户

Field 1) ID
Field 2) Password (it is stored with crypt)
Field 3) Status (ranges from 0-2)

的index.php

<?php
session_start();
unset($_SESSION['Status']);
?>
<head> 

  <title>Login Form</title>
  <link rel="stylesheet" type="text/css" href="login.css">
  <img src="logo1.jpg" style="float:left; width:490px; height:130px; margin-top: -70px;">
  <!--[if lt IE 9]><script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
</head>
<body>

<section class="container">
    <div class="login">
      <h1>Login</h1>
      <form action="process_login.php" method="POST"/>
        <div class="help-tip">
         <p>Enter the User ID and Password that you were given, in order to login. If you have forgotten your ID or Password, contact Admin</p>
</div>
          <p><input type="number"     name="ID"   value="" placeholder="ID*"       required autofocus></p>
        <p><input type="password" name="Password" value="" placeholder="Password*" required></p>
        <p class="submit"><input type="submit" name="commit" value="Login"></p>
    </form>
</div>

process_login.php

<?php
session_start();
?>
<?php
//Connect to host site and databse
include("functions.php");
// Fetching variables
$id = $_POST['ID'];
$pw = crypt($_POST['Password']);

//Find user details from User table using the username entered and comparing the entered password with the one retrieved form the user table

$UserValidate = mysqli_query ("SELECT * FROM User WHERE ID = '$id'") or die (mysqli_error());
$row = mysqli_fetch_array($UserValidate);
$CorrectId = $row['ID'];
$CorrectPw = $row['Password'];
$UserType = $row['Status'];

//check if ID in database
if ($id == $CorrectId) {
//check if password is assigned to that username and is correct
if ($pw == $CorrectPw) {
//check if user is standard user
if ($UserType == 0) {
    $_SESSION['CadetUser'] = $id;
header('http://****/calendar.php:'.$url);die();
if ($UserType == 1) {
$_SESSION['StaffUser'] = $id;
header('http://****/calendar_staff.php:'.$url);die();
if ($UserType == 2) {
$_SESSION['AdminUser'] = $id;
header('http://****/calendar_admin.php:'.$url);die();
}
}
else { 
  echo "Either your ID or Password is wrong";
  header('http://******/index.php:'.$url);die();
}
}
}
}
?>

更新我的问题是,使用正确的详细信息登录时,出现黑屏。 它只是在process_login.php处停止。我也将重定向更改为“ header ..........”,如建议的那样

对于重定向,您可以尝试

header('location:'.$url);die();

注意:在标头之前删除所有回显或打印,并确保在php开头标签之前没有空格

顺便说一句,您的SQL语句容易受到SQL注入的攻击,因为您直接将$ id放入了语句中。 使用参数和mysqli会更安全

这是您的代码-缩进,如您在帖子中输入的那样:

//check if ID in database
if ($id == $CorrectId) {
    //check if password is assigned to that username and is correct
    if ($pw == $CorrectPw) {
        //check if user is standard user
        if ($UserType == 0) {
            $_SESSION['CadetUser'] = $id;
            header('http://****/calendar.php:'.$url);
            die();
            if ($UserType == 1) {
                $_SESSION['StaffUser'] = $id;
                header('http://****/calendar_staff.php:'.$url);
                die();
            // <----- missing a closing brace
            if ($UserType == 2) {
                $_SESSION['AdminUser'] = $id;
                header('http://****/calendar_admin.php:'.$url);
                die();
            }
        }
        else { 
            echo "Either your ID or Password is wrong"; // you need to remove this; outputting HTML prior to sending headers will result in a PHP error
            header('http://******/index.php:'.$url);
            die();
        }
    }
}
} // <----- remove this

如您所见,唯一有机会的条件是if ($UserType == 0) 更不用说其中有一个错误的} ,它可能导致语法错误。

您的标题中也缺少Location ,例如。 header('Location: url/goes/here.php');

我在下面重新格式化了您的代码,并修复了语法错误:

//check if ID in database
if ($id == $CorrectId && $pw == $CorrectPw) {
    //check if user is standard user
    if ($UserType == 0) {
        $_SESSION['CadetUser'] = $id;
        header('Location: http://****/calendar.php:'.$url);
        die();
    }
    elseif ($UserType == 1) {
        $_SESSION['StaffUser'] = $id;
        header('Location: http://****/calendar_staff.php:'.$url);
        die();
    }
    elseif ($UserType == 2) {
        $_SESSION['AdminUser'] = $id;
        header('Location: http://****/calendar_admin.php:'.$url);
        die();
    }
    else { 
        header('Location: http://******/index.php:'.$url);
        die();
    }
}

并且由于if ($id == $CorrectId)if ($pw == $CorrectPw)是必须满足的条件才能继续进行,因此将它们包括在单个条件中是有意义的。 您应尽可能避免嵌套条件太深。 使事情变得凌乱,并且代码难以阅读/遵循。 您可以看到我已将它们添加到单个条件中。

更改标题功能

header('http://****/calendar.php:'.$url);

header('location : http://****/calendar.php:');

在标题中添加位置,如上所示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM