1
我想在5秒鐘後重定向用戶,此小代碼片段在5秒後重定向。使用輸出緩衝寫入後,使用標題功能重定向時遇到問題
<?php
ob_start();
echo "Thank you for your input";
header("Refresh: 5; url=http://www.google.com");
ob_end_flush();
?>
但是,當我嘗試將它實現到我的應用程序時,什麼都不會發生。這裏頭功能不起作用。
<?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();
?>
我已經嘗試在開始和結束分別放置ob_start()和ob_end_flush()。但是,這似乎仍然不起作用。
也許,如果它之前停止其沒有達到重定向線。你可以試着回聲一些類似'echo「的東西到達這裏」;'在這行之後: '$ insertationQuery = mysql_query($ insertation,$ connection);'並檢查它是否顯示? –
它出現在那行之後它只是標題不重定向 – johnbumble
標題應該在輸出之前出現。嘗試在你的echo語句之前放置標題 – theatlasroom