【问题标题】:header("Location: example.php"); not workingheader("位置:example.php");不工作
【发布时间】:2014-05-10 10:26:00
【问题描述】:

谁能告诉我为什么我的标头重定向不起作用?

这是我的代码:

<?php
include('includes/connect.php');
include('includes/link.php');
include('includes/time_stamp.php');

session_start();

$id = $_SESSION["id"];
$username = $_SESSION["username"];

$path = "uploads/";
$valid_formats = array("jpg", "png", "gif", "bmp", "jpeg");
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];

list($txt, $ext) = explode(".", $name);
$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
move_uploaded_file($tmp, $path.$actual_image_name);

$time=time();
$ip=$_SERVER['REMOTE_ADDR'];
mysql_query("INSERT INTO `messages` (message, id_fk, ip,created) VALUES ('$actual_image_name', '$id', '$ip','$time')");

header("Location: index.php");
?>

谢谢

【问题讨论】:

  • 您的脚本是否正确插入数据库?
  • 是的,数据库插入出现了。
  • 那么当你执行这个脚本时你在屏幕上看到了什么?
  • 首先将mysql_query 包装在一个条件中,甚至在重定向之前检查它是否执行或因异常而死。
  • 页面只是空白。

标签: php html mysql redirect url-redirection


【解决方案1】:

确保您在header("Location: index.php"); 之前没有回显任何内容或没有空格或尝试使用

echo("<script> top.location.href='index.php'</script>");

【讨论】:

    【解决方案2】:

    如果您仍然只看到白页,则必须回显错误。

    使用此打击代码,如果mysql_query 有效,则位置将更改。

    否则,您将看到错误消息。

    <?php
    include('includes/connect.php');
    include('includes/link.php');
    include('includes/time_stamp.php');
    session_start();
    $id = $_SESSION["id"];
    $username = $_SESSION["username"];
    $path = "uploads/";
    $valid_formats = array("jpg", "png", "gif", "bmp", "jpeg");
    $name = $_FILES['photoimg']['name'];
    $size = $_FILES['photoimg']['size'];
    list($txt, $ext) = explode(".", $name);
    $actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
    $tmp = $_FILES['photoimg']['tmp_name'];
    move_uploaded_file($tmp, $path.$actual_image_name);
    $time=time();
    $ip=$_SERVER['REMOTE_ADDR'];
    $sq = mysql_query("INSERT INTO `messages` (message, id_fk, ip,created) VALUES ('$actual_image_name', '$id', '$ip','$time')");
    if ($sq) {
    header("Location: index.php");
    }
    else {
        echo "SQL Error.";
    }
    ?>
    

    【讨论】:

      【解决方案3】:

      标题必须是发送到浏览器的第一件事(但它没有必须在顶部)。在您的情况下,请将其放在 PHP 脚本的开头,并确保在开始标记之前没有空格。

      【讨论】:

      • 这只是对了一半。您必须先将其发送到浏览器,是的,但您不必将其放在 php 脚本之上。您只需确保在发送标头之前没有其他输出(如回显、打印等)。
      • 我希望重定向在 mysql 插入之后发生。将其放在顶部会重定向。不过还是谢谢。
      • 无论如何都会执行 MySQL 插入操作。我没有说一定要放在最前面,而是建议把它放在最上面,以避免可能由次要功能引起的任何输出的并发症。
      • 已编辑,因为你们中的一些人似乎故意误读了我的声明。我从来没有说过它必须在脚本之上。
      【解决方案4】:

      在这种情况下,最好逐行调试。

      案例 1

      您能否在文件顶部启用错误报告,以便查看是否有任何错误消息。

      <?php
      ini_set('display_errors', 1 );
      ini_set( 'error_reporting', E_ALL );
      
      include('includes/connect.php');
      include('includes/link.php');
      include('includes/time_stamp.php');
      ?>
      

      如果没有错误,请继续执行步骤 2。

      案例 2

      <?php
      ini_set('display_errors', 1 );
      ini_set( 'error_reporting', E_ALL );
      
      include('includes/connect.php');
      include('includes/link.php');
      include('includes/time_stamp.php');
      
      header( 'Location: index.php' );
      exit(1); // always put this after header call to stop script execution.
      
      // comment every thing below it.
      ?>
      

      如果上述方法有效,则您的查询可能有错误。

      【讨论】:

        【解决方案5】:

        如果 mysql_query() 函数调用正确,请在将标头发送到浏览器进行重定向之前尝试使用输出缓冲

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-04-13
          相关资源
          最近更新 更多