header(“Location:example.php”);不工作

时间:2014-03-31 07:11:28

标签: php html mysql redirect url-redirection

任何人都可以告诉我为什么我的标题重定向不起作用?

这是我的代码:

<?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");
?>

由于

5 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分: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.";
}
?>

答案 2 :(得分:1)

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

答案 3 :(得分:0)

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

案例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.
?>

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

答案 4 :(得分:0)

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