简体   繁体   中英

PHP redirect page works only in localhost

I haven't found any solutions for my problem so I decided to ask. I would like to redirect my php script page after submitting a form, so I use header at the end of my codes! Interestingly, when I test it in my localhost, it works and redirects the page successfully, but, when I test it in my remote server it fails to redirect the page! and when I submit the form, it goes to the from processing script and show a blank page! I wonder if anyone can help me on this matter.

and here is my codes in the form processing script:

<?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);
}
}
?> 
<?php require_once('Connections/db.php'); ?>


<?php 

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

Change your above code to

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

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

and also remove the last ?> if you do not have html after that.

After that, it should work.


To turn on Error Reporting, place ini_set('display_errors',1); error_reporting(E_ALL); ini_set('display_errors',1); error_reporting(E_ALL); at the beginning of your php script, and you should be able to see the error.

More info about the problem you are facing here

You are opening and closing the php tags each time on single php file. just remove the closing tag form first line as:

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

and also remove the <?php tag from second line.

Change the header look like this will work. Or

header('Location: index.htm'); 

Or

HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM