【问题标题】:PHP header not working after uploading it to the serverPHP标头在上传到服务器后不起作用
【发布时间】:2014-12-12 09:19:45
【问题描述】:

这是我上传到我的网络服务器的简单代码,用于查找标题在主应用程序中不起作用的原因。即使这也不起作用,我正在使用 i 页面服务器。这在 localhost wamp 服务器中运行良好。

<?php 
 ob_start();

 header('Location: login-twitter.com');

 ob_end_flush();
?> 

我也试过下面的代码

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Untitled Document</title>
  </head>
  <body>
  <h1>Hello</h1> 
  <?php 
  ob_start();

   header('Location: login-twitter.com');

  ob_end_flush(); 
  ?> 
  </body>
  </html>

【问题讨论】:

  • 第二个不起作用,因为您已经向浏览器发送了标题(文本)。 (我认为)
  • ob_start() 对已经制作的内容没有任何权力。在从 PHP(echo()print()printf()readfile() 等)或嵌入式 HTML 输出任何内容之前,您需要将其放在脚本的开头。

标签: php html redirect header


【解决方案1】:

尝试使用 header('Location: http://www.login-twitter.com/');而不是 header('Location : login-twitter.com');

【讨论】:

  • 这是正确的用法。 Location: 标头的值必须是绝对 URI,即它必须以协议开头。 login-twitter.com 不是 URL,它只是一个主机名。
【解决方案2】:

在文件的最开始,添加以下行:

甚至在 HTML &lt;!Doctype&gt; 之前

<?php session_start(); 
ob_start();
?> 

注意:如果您在浏览器上有一些 HTML 输出,则不会发生重定向。

【讨论】:

  • doctype 之后没有。 之前。在任何 HTML 或您在该页面中生成的任何内容之前。
【解决方案3】:

您必须在 html 起始标记之前执行重定向( header('Location: login-twitter.com'); )。

【讨论】:

    【解决方案4】:

    试试这个:

    <script type="text/javascript">
      window.location.assign('http://www.login-twitter.com');
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      相关资源
      最近更新 更多