重定向不起作用,它什么都不做

时间:2016-01-12 09:44:50

标签: php

我的标题位置不起作用,在我的wampserver中它可以工作,当我把它放在我的服务器中时,它没有,任何想法?

如果我放置一个echo而不是标题,它就可以正常工作。

<html>
    <head>
        <meta charset="utf-8">
        <title>Login</title>
        <link rel="stylesheet" href="css/login.css" />
    </head>
    <body>
    <?php
        require('db.php');

        session_start();

        // If form submitted, insert values into the database.
        if (isset($_POST['username'])){
            $username = $_POST['username'];
            $password = $_POST['password'];

            $username = stripslashes($username);
            $username = mysqli_real_escape_string($connection, $username);

            $password = stripslashes($password);
            $password = mysqli_real_escape_string($connection, $password);

            $query = "SELECT * FROM `users` WHERE username='$username' and password='$password'";
            $result = mysqli_query($connection, $query) or die(mysql_error());
            $rows = mysqli_num_rows( $result);
            if($rows==1){
                $_SESSION['username'] = $username;
                header("Location: welkom.php");
            }else{
                echo "<div id='cancel'><h3>Username/password is incorrect.</h3><br/>Click here to <a href='index.php'>try again</a></div>";
            }
        }else{   
    ?>
    <div id="login">
        <div id="triangle"></div>
        <h1>Log in</h1>
        <form action="" method="POST" name="login">
            <input type="text" name="username" placeholder="Username" required />
            <input type="password" name="password" placeholder="Password" required />
            <input name="submit" type="submit" value="login" />
        </form>
    </div>
    <?php } ?>
    </body>
</html>

4 个答案:

答案 0 :(得分:1)

如果有的话,请确保在位置标题之前或之后没有任何输出,并在其后添加die() ....您的sessions_start()也应该无效,因为您之前已经输出了声明......

所以你的会话开始,你的位置标题应该在HTML之前。

看看我改变了什么:

<?php
        require('db.php');

        session_start();

        // If form submitted, insert values into the database.
        if (isset($_POST['username'])){
            $username = $_POST['username'];
            $password = $_POST['password'];

            $username = stripslashes($username);
            $username = mysqli_real_escape_string($connection, $username);

            $password = stripslashes($password);
            $password = mysqli_real_escape_string($connection, $password);

            $query = "SELECT * FROM `users` WHERE username='$username' and password='$password'";
            $result = mysqli_query($connection, $query) or die(mysql_error());
            $rows = mysqli_num_rows( $result);
            if($rows==1){
                $_SESSION['username'] = $username;
                header("Location: welkom.php");
                die();
            }
<html>
    <head>
        <meta charset="utf-8">
        <title>Login</title>
        <link rel="stylesheet" href="css/login.css" />
    </head>
    <body>
    <?php
        if($rows!=1){
                echo "<div id='cancel'><h3>Username/password is incorrect.</h3><br />Click here to <a href='index.php'>try again</a></div>";

        }else{   
    ?>
    <div id="login">
        <div id="triangle"></div>
        <h1>Log in</h1>
        <form action="" method="POST" name="login">
            <input type="text" name="username" placeholder="Username" required />
            <input type="password" name="password" placeholder="Password" required />
            <input name="submit" type="submit" value="login" />
        </form>
    </div>
    <?php } ?>
    </body>
</html>

答案 1 :(得分:0)

您无法使用header("Location: welkom.php");,因为标题已经发送。

之前的中的所有HTML加载到变量中,然后在代码达到该条件后将其回显,或者打开Output Buffering

或者,在将任何发送到浏览器之前,请重新订购代码以检查此情况并重定向。

答案 2 :(得分:0)

删除它:

header("Location: welkom.php");

试试这个:

echo '<script>window.location.href = "welkom.php";</script>' ;

答案 3 :(得分:-1)

在标题位置使用完整网址,即header("Location: httP://yoursite.com/welkom.php"); 这将有效。