我在本地使用WAMP服务器构建了一个网站,一切正常,然后移植到GoDaddy上的Web服务器,我的重定向功能突然停止工作。
(www.minute.tech)的简单站点,您可以测试我的一个表单,转到form_processing.php,但应该重定向到带有消息的索引页面。当您在失败的重定向后返回索引页面时,它仍会显示" Booyah!..."的正确消息,并将电子邮件输入我的数据库。
为什么它不能在我的网络服务器上重定向的任何想法,但在我的本地WAMP服务器上会使用相同的代码?干杯!
这是我在sessions.php中的重定向功能:
function redirect_to($new_location) {
header("Location: " . $new_location);
exit();
}
这里是我重定向的process_email.php:
<?php require_once("sessions.php"); ?>
<?php require_once("db_connection.php"); ?>
<?php require_once("functions.php"); ?>
<?php
if(isset($_POST['submit']) && !empty($_POST['email'])){
//Process form
$email = $_POST['email'];
$email = mysql_prep($email);
$techcheck = (isset($_POST['techcheck'])) ? 1 : 0;
// 2. Perform database query
$query = "INSERT INTO signups (";
$query .= " email, techcheck";
$query .= ") VALUES (";
$query .= "'{$email}', $techcheck";
$query .= ")";
$result = mysqli_query($connection, $query);
if ($result) {
// Success
$_SESSION["good_message"] = "Booyah! We will keep you posted on progress.";
redirect_to("../index.php");
} else {
//Failure
$_SESSION["bad_message"] = "Failed to accept email.";
redirect_to("../index.php");
}
} else {
//This can be an accidental GET request
$_SESSION["bad_message"] = "That is not a valid email! Please try again.";
redirect_to("../index.php");
}
?>
答案 0 :(得分:0)
有时你必须为头功能启动对象
将此代码添加到所有页面
<?php ob_start(); ?>
答案 1 :(得分:0)
删除所有空格。在标题重定向之前,某些服务器设置可以输出,但绝大多数服务器都不会正确重定向。打开错误报告,它可能会告诉您在重定向之前有输出:
<?php
// ^---- Just do one open tag
require_once("sessions.php"); // Remove close tag, possible empty space after
require_once("db_connection.php"); // Remove close tag, possible empty space after
require_once("functions.php"); // Remove close tag, possible empty space after
// Remove the close and open php tags
if(isset($_POST['submit']) && !empty($_POST['email'])){
//Process form
$email = $_POST['email'];
$email = mysql_prep($email);
$techcheck = (isset($_POST['techcheck'])) ? 1 : 0;
// 2. Perform database query
$query = "INSERT INTO signups (";
$query .= " email, techcheck";
$query .= ") VALUES (";
$query .= "'{$email}', $techcheck";
$query .= ")";
$result = mysqli_query($connection, $query);
if ($result) {
// Success
$_SESSION["good_message"] = "Booyah! We will keep you posted on progress.";
redirect_to("../index.php");
} else {
//Failure
$_SESSION["bad_message"] = "Failed to accept email.";
redirect_to("../index.php");
}
} else {
//This can be an accidental GET request
$_SESSION["bad_message"] = "That is not a valid email! Please try again.";
redirect_to("../index.php");
}
// If you have no more content below this point, just remove the close php tag
// it is not required and is a possible source of empty space down the line...
此外,您不应再使用mysql_
,它已在PHP 7中弃用并删除。另外,绑定参数而不是将此参数直接放入sql中:
$query .= "'{$email}', $techcheck";
答案 2 :(得分:0)
使用JS。
<?php
function redirect_to($new_location) { ?>
<script>window.location="<?php echo $new_location; ?>";</script>
<?php } ?>