dongtang1909 2016-06-21 22:43
浏览 46

在使用输出缓冲写入后无法使用标头功能重定向

I want to redirect the user after five second this small snippet of codes redirects after 5 seconds.

   <?php
    ob_start();

    echo "Thank you for your input";
    header("Refresh: 5; url=http://www.google.com");
    ob_end_flush();
     ?>

But When I try to implement it into my application nothing happens. here the header function doesn't work.

<?php 

$name = "";
$email = "";
$subject = "";
$comments = "";
$nameError = "";
$emailError = "";
$subjectError = "";
$timestamp = date("Y-m-d H:i:s");
$x = 5;
$userIP = $_SERVER['REMOTE_ADDR'];
function filterData($data)  {
$data = mysql_real_escape_string($data);
return $data;
}
$connection = mysql_connect('----', '----', '-----'); 

$select_database = mysql_select_db("contact"); 

if ($_SERVER["REQUEST_METHOD"] == "POST") {
//handles the name 
$name = filterData($_POST["name"]);
if (empty($name)) {
$nameError = "please don't leave the name field blank";
}

//handles the email
$email = filterData($_POST["email"]);
if (empty($email)) {
$emailError = "please don't leave the email field blank";
}

//handles the subject
$subject = filterData($_POST["subject"]);
if (empty($subject)) {
$subjectError = "please don't leave this field blank";
}

$comments = filterData($_POST["comments"]);

}

$insertation = "INSERT INTO contactinfo (name, email, subject, date, comments, ip)
VALUES ('$name', '$email', '$subject', '$timestamp', '$comments', '$userIP')";

$insertationQuery = mysql_query($insertation, $connection);

ob_start();

echo "Thank you for your input";
header("Refresh: 5; url=http://www.google.com");
ob_end_flush();

?> 

I have tried putting ob_start() and ob_end_flush() at the start and end respectively. But this still doesn't seem to work.

  • 写回答

1条回答 默认 最新

  • dongshan7060 2016-06-21 23:08
    关注

    header must be called before anything is echoed. So place your echo after header.

    header("Refresh: 5; url=http://www.google.com");
    echo "Thank you for your input";
    

    http://php.net/manual/en/function.header.php#97114

    评论

报告相同问题?

悬赏问题

  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!