标头重定向不在服务器上工作但在localhost上工作

Sam*_*pez 6 php

我使用bluehost作为我的webhost,我遇到了一些问题.下面的代码是我的困惑点.

<?php
include '../init.php';
error_reporting(0);
?>
<div class='container'>
<html>
<head>
<link rel="stylesheet" type="text/css" href="/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="/bootstrap/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="/bootstrap/css/responsive.css">    
<link rel="stylesheet" type="text/css" href="/bootstrap/css/responsive.min.css">
<script language="JavaScript" type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<link rel="stylesheet" type="text/css" href="SignIn.css">
<title>SoundBooth - Sign in</title>
<link rel="icon" href="../Images/Logo.png" type="image/x-icon"/>
</head>
<body  background="../Images/BGMain.png">
<div class='signinLabel'>Sign in</div>
<div class='bg'></div>
<div class='user'>Username</div>
<div class='pass'>Password</div>
<form action="index.php" method='POST'>
  <input name='Username' autocomplete="off"class='usr' type="text" style='height:34px;'/>
  <input name='Password' autocomplete="off"class='pas'type="password" style='height:34px;'/>

  <input type='submit' class='submit' value='Log in' ></input>
</form>
<a class='forgotPass' href="#">(Forgot password)</a>
<div class='no-account-label'>Don't have an account? |</div>
<a class='no-account-button' href="/signup">Sign up</a>
<?php
 if (empty($_POST) === false){
    $username = $_POST['Username'];
    $password = $_POST['Password'];

    if (empty($username) || empty($password)){ ?>
            <div style="float:left;margin:-415px 200px;width:500px;text-indent:100px;"class="alert alert-error">You need to enter a username and password!</div>
            <?php
        }elseif (user_exists($username) === false){?>
            <div style="float:left;margin:-415px 170px;width:500px;text-indent:100px;"class="alert alert-error">We can't find that username! Have you <a style='color: rgb(185, 74, 72);' href='../HTML/signup.html'>registered?</a></div>
            <?php
        }elseif (user_active($username) === false){ ?>
            <div style="float:left;margin:-415px 200px;width:500px;text-indent:100px;"class="alert alert-error">You haven't activated your account!</div> <!--Remove this-->
            <?php
        }else{

            $login = login($username, $password);
            if ($login === false){ ?>
            <div style="float:left;margin:-415px 200px;width:500px;text-indent:100px;"class="alert alert-error">The username or password you have entered is incorrect!</div>
            <?php
            }else{
                $errors[] = 'Logging In...';
                $_SESSION['user_id'] = $login;
                if (isset($_SESSION['user_id'])){
                mysql_query("UPDATE `users` SET `online_offline`='1' WHERE `user_id`='$_SESSION[user_id]'");
                }
                header("Location: http://sound-booth.com/");
                exit();
            } 
        }
    }
    echo output_errors($errors)
?>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

是脚本的链接.它适用于localhost,但不适用于我的实际服务器.即使header()重定向也不起作用.

有人可以解释一下吗?

解:

ob_start();
Run Code Online (Sandbox Code Playgroud)

Sri*_*Rao 9

最好的选择就是这样使用.

// Use this line instead of header
echo "<script>location='your_url.com'</script>";
Run Code Online (Sandbox Code Playgroud)


Hav*_*ard 6

必须在发送任何输出之前设置标头.在进行该header()调用之前,您正在向用户发送大量HTML .也许它在一个环境中工作但在另一个环境中工作,因为它output_buffering是在您的localhost php.ini上设置的,而不是在服务器上.

您应该重新编写代码以确保header()在任何内容之前发送,但如果您希望将其工作添加ob_start()到代码的开头,则会强制执行缓冲.


Dun*_*dnd 0

您可能关闭了错误报告,这就是您不会收到错误消息的原因。

如果 header() 前面有一个空格,则它不起作用。您需要将其放在所有字符串输出之上才能使其正常工作。