使用PHP进行简单重定向 - 不会重定向

时间:2014-05-25 19:35:09

标签: php redirect login

如果可以,请帮助我,我很欣赏它。

我有两个文件 - index.php和home.php,当前类型的代码工作,如果我将重定向更改为echo'login successful'之类的东西它可以工作,但是当我将重定向代码放回去时它会不

的index.php

      <link rel="stylesheet" type="text/css" href="css/index.css"/>
<html>
<head>
    <title>SG - 2014</title>
</head>
<body background="images/strainsbackground.jpg">
<div id="allcolour">
<body>
<h1></h1>
<?php
if (!isset($_POST['submit'])){
?>
<a href="/register.php" style="text-decoration:none">
      <button  label="Register" name="so_link">Register</button>
</a>
<div id ="formcolour">
<!-- The HTML login form -->
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
        Username: <br><input type="text" name="username" /><br />
        Password: <br><input type="password" name="password" /><br />
        <input type="submit" name="submit" value="Login" />

        <a href="register.php">Register</a>
    </form>
    </div>

<?php
} else {
    require_once("db_const.php");
    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    # check connection
    if ($mysqli->connect_errno) {
        echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
        exit();
    }

    $username = $_POST['username'];
    $password = $_POST['password'];
    $sql = "SELECT * from users WHERE username LIKE '{$username}' AND password LIKE '{$password}' LIMIT 1";
    $result = $mysqli->query($sql);
    if (!$result->num_rows == 1) {
         echo "Invalid username/password combination";
    } else {
    $_SESSION['user'] = $_POST['username'];
        header('location:home.php');
    }}


?>  

</body>
</div>
</div>
</html>

由于

3 个答案:

答案 0 :(得分:0)

您尝试在已发送标头后设置标头。

您的代码:

Output some html
Trying to set header LOCATION
More HTML

通过发送HTML发送标题,您发送后无法管理标题。这就是为什么你的代码不起作用。

这样做:

Set header LOCATION
Output some html

所以你的代码将是:

<?php
if (isset($_POST['submit'])){
    require_once("db_const.php");
    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    # check connection
    if ($mysqli->connect_errno) {
        echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
        exit();
    }

    $username = $_POST['username'];
    $password = $_POST['password'];
    $sql = "SELECT * from users WHERE username LIKE '{$username}' AND password LIKE '{$password}' LIMIT 1";
    $result = $mysqli->query($sql);
    if (!$result->num_rows == 1) {
         echo "Invalid username/password combination";
    } else {
    $_SESSION['user'] = $_POST['username'];
        header('location:home.php');
    }}


?>  

      <link rel="stylesheet" type="text/css" href="css/index.css"/>
<html>
<head>
    <title>SG - 2014</title>
</head>
<body background="images/strainsbackground.jpg">
<div id="allcolour">
<body>
<h1></h1>
<?php
if (!isset($_POST['submit'])){
?>
<a href="/register.php" style="text-decoration:none">
      <button  label="Register" name="so_link">Register</button>
</a>
<div id ="formcolour">
<!-- The HTML login form -->
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
        Username: <br><input type="text" name="username" /><br />
        Password: <br><input type="password" name="password" /><br />
        <input type="submit" name="submit" value="Login" />

        <a href="register.php">Register</a>
    </form>
    </div>

<?php
}
?>  

</body>
</div>
</div>
</html>

答案 1 :(得分:0)

而不是:

header('location:home.php');

使用:

echo <<<EOT

<script>
document.location = 'home.php';
</script>

EOT;

是的,对PHP来说太晚了,所以使用Javascript重定向。

或者,将PHP脚本移动到文件的顶部。 (更新为包含“session_start()”。)

的index.php:

<?php

session_start();

if (isset($_POST['submit'])){
    require_once("db_const.php");
    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    # check connection
    if ($mysqli->connect_errno) {
        echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
        exit();
    }

    $username = $_POST['username'];
    $password = $_POST['password'];
    $sql = "SELECT * from users WHERE username LIKE '{$username}' AND password LIKE '{$password}' LIMIT 1";
    $result = $mysqli->query($sql);
    if (!$result->num_rows == 1) {
        $error_msg = 'Invalid username/password combination';
    } else {
        $_SESSION['user'] = $_POST['username'];
        header('location:home.php');
    }
}
?>  
<html>
<head>
    <title>SG - 2014</title>
    <link rel="stylesheet" type="text/css" href="css/index.css"/>
</head>
<body background="images/strainsbackground.jpg">
<div id="allcolour">
<body>

<?=$error_msg?>

<h1></h1>
<a href="/register.php" style="text-decoration:none">
      <button  label="Register" name="so_link">Register</button>
</a>
<div id ="formcolour">
<!-- The HTML login form -->
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
        Username: <br><input type="text" name="username" /><br />
        Password: <br><input type="password" name="password" /><br />
        <input type="submit" name="submit" value="Login" />

        <a href="register.php">Register</a>
    </form>
    </div>    
</body>
</div>
</div>
</html>

home.php:

<?php

session_start();

if(!isset($_SESSION['user'])) {
    header('location:index.php');
}

// home

?>

答案 2 :(得分:0)

我还没有阅读所有代码,但我认为问题在于这一行:

if (!$result->num_rows == 1) {

尝试将其更改为:

if ($result->num_rows !== 1) {