PHP重定向页面仅适用于localhost

时间:2015-06-16 05:42:37

标签: php mysql forms redirect

我没有找到解决问题的方法所以我决定问一下。我想在提交表单后重定向我的PHP脚本页面,所以我在我的代码末尾使用标题!有趣的是,当我在我的localhost中测试它时,它可以正常工作并重定向页面,但是,当我在远程服务器上测试它时,它无法重定向页面!当我提交表单时,它会转到处理脚本并显示空白页面!我想知道是否有人可以帮助我解决这个问题。

以下是表单处理脚本中的代码:

<?php require_once('Connections/db.php'); ?>


<?php 

if (isset($_POST['submit']))
{
$name = mysqli_real_escape_string($connection, $_POST['name']);
$email = mysqli_real_escape_string($connection, $_POST['email']);
$subject = mysqli_real_escape_string($connection, $_POST['subject']);
$message = mysqli_real_escape_string($connection, $_POST['message_area']);

$sql = "INSERT INTO contact_messages (name, email, subject, message)

VALUES ( '$name', '$email', '$subject','$message')";

$result = mysqli_query($connection, $sql);

if($result){
header('Location: http://www.mylink/index.htm'); 
exit();
} 
else{
    echo "ERROR: Could not execute the form " . mysqli_error($connection);
}
}
?> 

3 个答案:

答案 0 :(得分:3)

<?php require_once('Connections/db.php'); ?>


<?php 

if (isset($_POST['submit']))
{

将您的上述代码更改为

<?php require_once('Connections/db.php'); 

if (isset($_POST['submit']))
{

如果之后没有html,也会删除最后一个?>

之后,它应该有用。

要启用错误报告,请将ini_set('display_errors',1); error_reporting(E_ALL);放在php脚本的开头,您应该能够看到错误。

有关您遇到的问题的更多信息here

答案 1 :(得分:0)

每次在单个php文件上打开和关闭php标记。 只需将第一行的结束标记删除为:

<?php require_once('Connections/db.php'); 

并从第二行删除<?php标记。

答案 2 :(得分:0)

更改标题看起来像这样会起作用。或者

header('Location: index.htm'); 

HTTP / 1.1需要绝对URI作为»Location的参数:包括方案,主机名和绝对路径,但某些客户端接受相对URI。您通常可以使用$ _SERVER ['HTTP_HOST'],$ _SERVER ['PHP_SELF']和dirname()自己创建一个相对的URI:

<?php
/* Redirect to a different page in the current directory that was requested */
$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
?>