0

Possible Duplicate:
Headers already sent by PHP

Here is my current script. Instead of redirecting like I would like it to, the page just refreshes to the current page. The page used to work, but stopped working recently for no reason. I'm on PHP 5.3

<?php
$page = "Edit Topic";
require('top.php');
$var = safe($_GET['id']);
$q = mysql_query("SELECT * FROM `topics` WHERE `id`='".$var."'");
$r = mysql_fetch_array($q);
$c = mysql_num_rows($q);
$user = mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id`='".$_SESSION['id']."'"));

if($c==0) {
     echo "<p>This topic does not exist.</p>";
} elseif($_SESSION['name'] != $r['poster'] && $user['rank'] == "Member") {
     echo "<p>This is not your topic.</p>";
} else {
     if(isset($_POST['enter'])) {
          $title = safe($_POST['title']);
          $post = safe($_POST['post']);

          if($post=='' || $title=='') {
               $message = "You have to write something in your title and post.";
          } else {
               mysql_query("UPDATE `topics` SET `text`='".$post."' WHERE `id`='".$r['id']."'");
               mysql_query("UPDATE `topics` SET `title`='".$title."' WHERE `id`='".$r['id']."'");
               header("Location: http://www.domain.com/view-post/".$r['id']."");
          }
     }
}
?>
Community
  • 1
  • 1

4 Answers4

2

You can't produce any other output before calling header. Your problem is this line:

echo "<h1>Edit Topic</h1>";
Community
  • 1
  • 1
Gordon Bailey
  • 3,881
  • 20
  • 28
  • Did not work. Moved down Edit Topic into HTML and added full domain into header and still does not redirect. – Bryan Parmenter Jan 12 '13 at 22:23
  • Does `top.php` produce any output? That would also break it. Also, if you have any whitespace or other characters before the opening `` in this file or `top.php` that will break it too. – Gordon Bailey Jan 12 '13 at 22:36
  • It turns out that top.php did contain HTML output after reading the link you gave me. I used the script at this post to fix my problem. http://stackoverflow.com/a/8689521/1457548 – Bryan Parmenter Jan 12 '13 at 22:40
1
  1. exit() after header()
  2. verify that your top.php does no output (because you can't change header AFTER writing something to the client use the @ before mysql_-calls (like @mysql_query)
  3. use prepared statements and the mysqli-extension from php
  4. try to use utf-8-files without BOM, they are making a lot of people crazy like hell
FibreFoX
  • 2,858
  • 1
  • 19
  • 41
0

You have an echo before sending header. Remove the lines like: echo "<h1>Edit Topic</h1>"; header function works only before first output.

Vyacheslav Voronchuk
  • 2,403
  • 19
  • 16
0

You should use an absolute url instead of your relative one, and also exit() after header().

Tamás Pap
  • 17,777
  • 15
  • 70
  • 102