【问题标题】:header redirect not working on server but working on localhost标头重定向不在服务器上工作,但在本地主机上工作
【发布时间】:2013-03-14 00:00:28
【问题描述】:

我正在使用 bluehost 作为我的虚拟主机,但遇到了一些问题。下面的代码是我的困惑点。

<?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>

是脚本的链接。它适用于本地主机,但不适用于我的实际服务器。甚至 header() 重定向都不起作用。

有人可以解释一下吗?

解决方案:

ob_start();

【问题讨论】:

  • 这是一个 URL,不是代码。可以加代码吗?
  • 你去,我添加了它:)
  • Headers already sent by PHP 的可能重复项
  • 寻求帮助时/之前的第一步是 NUKING 语句,如 error_reporting(0);....

标签: php


【解决方案1】:

在页面的第一行使用

<php ob_start();?>

然后下面的头文件使用

ob_end_flush();

例如这样。

   <?php
ob_start();
?>
<?php
//Get values from the post Operation
require_once 'db/conn.php';

    if (isset($_POST['submit'])) {

        //extract values from the $_POST array
        $id = $_POST['id'];
        $fname = $_POST['firstName'];
        $lname = $_POST['lastName'];
        $dob = $_POST['dob'];
        $email = $_POST['email'];
        $contact = $_POST['phone'];
        $specialty = $_POST['specialty'];

        //call crud Funtion
        $result = $crud->editAttendees($id, $fname, $lname, $dob, $email, $contact, $specialty);
        //redirect  to index.php
        if ($result) {
        header("Location: viewrecords.php");
        ob_end_flush();
        
        } else {
            echo "error";
        }
    } 

【讨论】:

  • 使用 ob_end_flush();在 php 的末尾不起作用,因为@Vibhu 建议在 header('location index.php'); 之后使用 at ob_end_flush();它就像一个魅力。谢谢
【解决方案2】:

使用 ob_start(); 紧跟在 &lt;?php 之后,并在文档末尾使用 ob_end_flush();

【讨论】:

  • 我在ifs 中有多个标头重定向。但这在这里不起作用。我该怎么做才能让它发挥作用?
【解决方案3】:

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

您应该重新编写代码以确保在发送任何内容之前发送header(),但如果您只想让它正常工作,请将ob_start() 添加到代码的开头,它将强制缓冲。

【讨论】:

    【解决方案4】:

    最好的选择是这样使用。

    // Use this line instead of header
    echo "<script>location='your_url.com'</script>";
    

    【讨论】:

    • 在使用 url 的情况下,此代码有效。但是如果我使用 php 方法而不是 url,它会给出 Internal Server Error。我想使用这样的php方法重定向到另一个页面,echo "&lt;script&gt;location='$initResponse-&gt;getPaymentPageUrl()'&lt;/script&gt;";请给出想法..
    【解决方案5】:

    我认为ob_start() 函数调用是解决这个问题的最佳方法。头函数可以在逻辑语句之后调用他的属性。这是传递值,只是取它以前的值。

    参考:https://www.php.net/manual/en/function.ob-start.php

    【讨论】:

      【解决方案6】:

      HTTP 标头必须在 HTTP 响应之前发送。即,您必须调用header回显其他任何内容或发送任何 HTML。

      很有可能,它在您的本地服务器上运行的原因是因为您在那里配置了output buffering。您可以在页面顶部插入对ob_start(); 的调用,以在脚本中启用输出缓冲。然后,由于 HTML 将被缓冲直到脚本完成,重定向标头可以首先到达浏览器,这是必须的。或者,您可以重新编写脚本以将 header 调用放在顶部。

      【讨论】:

      • 由于某种奇怪的原因,没有任何效果,并给出了相同的结果。
      • @SammyLopez 我确定一定有错误消息。将error_reporting0 转到-1(全部),看看PHP 说的哪里出错了。
      • 错误:PHP 警告:无法修改标头信息 - 标头已由 /home1/soundbo3/public_html/login/index 中的(输出开始于 /home1/soundbo3/public_html/header.php:1)发送.php 在第 58 行
      • @SammyLopez 那么这就是你的答案。在header 调用之前,您不能输出anything。但是 header.php 的第 1 行正在这样做。您需要阻止它这样做,可能是在您想要重定向时不包括该文件,或者将开始输出缓冲的调用移动到 very 开头。
      【解决方案7】:
      header("Location: http://sound-booth.com/");
      

      它在文本输出之后进行。

      使用 OB_start() 或更改您的代码。

      【讨论】:

      • 如何集成 ob_start()?
      • 检查我的答案,它解释了 ob_start() 的使用
      • @Sammy Lopez:将ob_start()放在php代码的开头,ob_end_clean();放在末尾。这应该足够了。并删除&lt;html&gt;之前的&lt;div&gt;
      【解决方案8】:

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

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

      【讨论】:

      • “空白”到底是什么意思?
      • 空白可以是任何肉眼看不到的东西。例如换行,或两个单词之间的空格。所以本质上,你不能在 header() 之前输出任何东西
      【解决方案9】:

      OP,

      你有&lt;div class='container'&gt; 之前你的打开&lt;html&gt;标签(然后在&lt;/body&gt;之前关闭。

      打开&lt;body&gt; 标签后尝试移动&lt;div class='container'&gt;

      希望对您有所帮助。

      【讨论】:

        【解决方案10】:

        我也遇到了同样的问题。最后一个答案对我有用。

        echo "location='https://mysiteurl.in'";

        感谢 Jon Surrell。它甚至还带有会话值。

        【讨论】:

          【解决方案11】:
          $r=mysqli_query($con,$stmt);
          if(!$r)
          {
              die("Server failed" . mysqli_error($con));
          }
          else{
          header("Location: index.html");
          exit();
          }?>
          

          上面的代码对我有用。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-08-04
            • 2014-01-15
            • 1970-01-01
            相关资源
            最近更新 更多