user1334969
user1334969

Reputation: 57

PHP header not working for server but works perfectly fine on local host

This code below works perfectly fine when I try on my local host but when I upload it on to my server it does not work

$sendquery = "INSERT INTO `message` (`from`, `to`, `subject`, `date`,`message`) VALUES ('".$_SESSION['UID']."', '".$toarray['Uid']."', '$subject','$d', '$message')";
$sendresult = mysql_query($sendquery);

if($sendresult)
{
    header('Location: home.php?action=inbox&success=1');

}
else
{
    header("Location: home.php?action=inbox&success=2");
}

Is it a problem with my server or the code?

Upvotes: 1

Views: 7120

Answers (4)

a45b
a45b

Reputation: 519

Check

var_dump($sendresult); exit();

Enable error reporting. You may have other error. By the way

$sendresult = 1;

if($sendresult) {

header('Location: home.php?action=inbox&success=1');

} else {

header("Location: home.php?action=inbox&success=2");

}

This works fine for me.

Upvotes: 0

CodeZombie
CodeZombie

Reputation: 5377

Make sure nothing in your code started output before you set the "Location" header.
Make sure you use an absolute URL (as required by the HTTP specification)

Sample:

header('Location: http://' . $_SERVER['HTTP_HOST'] . '/Home.php');

If this is the case, your redirect should work.

Some common reasons why output already started:

  • BOM (Byte order mark) not handled correctly
  • Warning or information
  • Whitespaces or any other HTML markup before your first <?php ... ?>

Upvotes: 1

gulyan
gulyan

Reputation: 662

Maybe you have an error in the SQL.

If you echo something (like an error message) you can't change the header anymore.

The header is send before the echo and setting the location after that has no effect.

Upvotes: 1

Starx
Starx

Reputation: 78941

The localhost and webserver have many differences together. I am guessing the problem to be developed on windows and hosted on linux.

Lowercase all the header string and stop the execution after it

header("location: home.php?action=inbox&success=2");
exit();

Upvotes: 0

Related Questions