登录成功后,标题不会重定向

时间:2013-11-22 20:15:46

标签: php html mysql

我正在尝试完成登录系统。它可以正常工作,而不是通过标题功能重定向。我已经完成了调试,以显示它在登录时到达头函数但是头函数没有执行。

以下是登录页面的代码。我将它设置为加载页面,然后在提交表单时重新加载并执行php代码。

<?php
    //Include database connection details
    require_once('connection.php');

    //Array to store validation errors
    $errmsg_arr = array();

    //Validation error flag
    $errflag = false;

    //Function to sanitize values received from the form. Prevents SQL injection
    function clean($str) {
        $str = @trim($str);
        if(get_magic_quotes_gpc()) {
            $str = stripslashes($str);
        }
        return mysql_real_escape_string($str);
    }

    //Sanitize the POST values
    $username = clean($_POST['username']);
    $password = clean($_POST['password']);

    //Input Validations
    if($_SERVER["REQUEST_METHOD"] == "POST")
    {
        if(empty($username)) {
            $usernamerr = 'Username missing';
            $errflag=true;
        }
        if(empty($password)) {
            $passerr = 'Password missing';
            $errflag=true;

        }
    }

    $qry="SELECT * FROM PERSON WHERE Username='$username' AND Pword = '$password'";
    $result=mysql_query($qry);

    //Check whether the query was successful or not
    if($result) {
        if(mysql_num_rows($result) > 0) {
            //Login Successful

            $query="SELECT * FROM GROUPING WHERE Username='$username'";
            $result=mysql_query($query);
            $group = mysql_fetch_assoc($result);


            if($group['Admin_flag'] === '1')
            {
                header("location:homeAdmin.php");
                exit();
            }
            else{
                header("location:home.php");
                exit();
            }
            exit();
        }else {
            //Login failed
            if($_SERVER["REQUEST_METHOD"] == "POST" && !$errflag){
            $queryerr = 'user name and password not found';
            }
        }
    }
?>
<html>

<head>
    <link href="styles/stylesheet.css" rel="stylesheet" type="text/css">
</head>

<body>

<h1 align=center class="green">Welcome to the Schneider Electric Login Page.</h1>


<form name="loginform" action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
<table width="309" border="0" align="center" cellpadding="2" cellspacing="5">
  <tr>
    <td colspan="2">
        <ul class="err">
        <li><?php echo $usernamerr;?></li>
        <li><?php echo $passerr;?></li>
        <li><?php echo $queryerr;?></li>
    </td>
  </tr>
  <tr>
    <td width="116"><div align="right" class="arial">Username</div></td>
    <td width="177"><input name="username" type="text" /></td>
  </tr>
  <tr>
    <td><div align="right" class="arial">Password</div></td>
    <td><input name="password" type="password" /></td>
  </tr>
  <tr>
    <td><div align="right"></div></td>
    <td><input name="" type="submit" value="login" /></td>
  </tr>
</table>
</form>
</body>

</html>

这是在开头包含的connection.php

<?php
$mysql_hostname = "localhost";
$mysql_user = "user";
$mysql_password = "password";
$mysql_database = "database";
$prefix = "";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database) or die("Could not select database");
?>

3 个答案:

答案 0 :(得分:1)

为了使HTTP标头操作正常工作,您的脚本必须尚未向客户端发送任何其他输出。

处理此问题的典型方法是使用输出缓冲。

查看ob_start()ob_end_flush()

因此,在执行任何其他操作之前,您需要在脚本开头调用ob_start。然后,脚本的最后一部分应该调用ob_end_flush()来实际将输出发送到客户端。

<?php
     ob_start();

      //Include database connection details
     require_once('connection.php');

     //Array to store validation errors
     $errmsg_arr = array();

     // ...  the rest of your script follows ...

     // ...  at some point you call the header function to redirect
 ?>
 <html>

 <head>
     <link href="styles/stylesheet.css" rel="stylesheet" type="text/css">
 </head>

 <body>
      <h1 align=center class="green">Welcome to the Schneider Electric Login Page.</h1>
       <form name="loginform" action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">

         <!--   The rest of your HTML form goes here -->

      </form>
  </body>

  </html>

<?php
    ob_end_clean();
?>

答案 1 :(得分:0)

您可以尝试在位置大写的L:位置。如果这不起作用,请尝试添加homeAdmin.php和home.php文件的完整路径:

header('Location: http://www.domain.com/homeAdmin.php');

header('Location: http://www.domain.com/home.php');

答案 2 :(得分:0)

这对我工作正常..添加 ob_start(); 并删除空格并将标题更改为 L 并在位置后提供一个空格

<?php
ob_start();
//Include database connection details
require_once('connection.php');

//Array to store validation errors
$errmsg_arr = array();

//Validation error flag
$errflag = false;

//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
    $str = @trim($str);
    if(get_magic_quotes_gpc()) {
        $str = stripslashes($str);
    }
    return mysql_real_escape_string($str);
}

//Sanitize the POST values
$username = clean($_POST['username']);
$password = clean($_POST['password']);

//Input Validations
if($_SERVER["REQUEST_METHOD"] == "POST")
{
    if(empty($username)) {
        $usernamerr = 'Username missing';
        $errflag=true;
    }
    if(empty($password)) {
        $passerr = 'Password missing';
        $errflag=true;

    }
}

$qry="SELECT * FROM PERSON WHERE Username='$username' AND Pword = '$password'";
$result=mysql_query($qry);

//Check whether the query was successful or not
if($result) {
    if(mysql_num_rows($result) > 0) {
        //Login Successful

        $query="SELECT * FROM GROUPING WHERE Username='$username'";
        $result=mysql_query($query);
        $group = mysql_fetch_assoc($result);


        if($group['Admin_flag'] === '1')
        {
            header("Location: homeAdmin.php");
            exit();
        }
        else{
            header("Location: home.php");
            exit();
        }
        exit();
    }else {
        //Login failed
        if($_SERVER["REQUEST_METHOD"] == "POST" && !$errflag){
        $queryerr = 'user name and password not found';
        }
    }
  }
 ?>