0

I have a page structured like below and I am having issues getting the redirect to work. This page gets an ID from the URL and uses it in the query. If there isn't a match, just redirect to another page.

I am getting the "headers already sent error," due to the include. I need the include to be there regardless. Is there a different way I can do the redirect if the query result is empty?

include('somepage.php');

$id = $_GET['id'];
$query = mysql_query("My query is here");

if(mysql_num_rows($query)==0) { header('Location:htp://example.com'); }

I've tried using exit(); and the various stop processing functions.

somepage.php:

<html>
<head>
(standard html)
include('sql-connect.php');
</head>

<body>
(code to format the header portion of the site)
Matt.
  • 1,306
  • 2
  • 13
  • 20

4 Answers4

2

You can put ob_start() at the beginning of the file, so that it looks like this:

<?php
ob_start();
include 'somepage.php';

$id = $_GET['id'];
$query = mysql_query("My query is here");

if(mysql_num_rows($query)==0) { header('Location:http://example.com'); }

Also, you can echo html redirect:

<?php
if(mysql_num_rows($query)==0) { echo '<meta http-equiv="refresh" content="0; url=http://example.com/">'; die(); }
Martin Janeček
  • 560
  • 4
  • 20
1

You need to add ob_start() at the beginning of the file and if this still does not work then you also need to add ob_flush(); to fully flush out the old header.

    flush(); // Flush the buffer
    ob_flush();
    header("Location: http://example.com");
Hamza
  • 1,593
  • 2
  • 19
  • 31
  • Thanks! That is correct, same as Martin said before you. – Matt. Feb 02 '14 at 18:11
  • 2
    Just as a knowledge, read the answer by Mario at http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Hamza Feb 02 '14 at 18:13
0

You may have a blank line after a closing ?> This will cause some literal whitespace to be sent as output, preventing you from making subsequent header calls. Note that it is legal to leave the close ?> off the include file, which is a useful idiom for avoiding this problem.

ob_start() may fix your problem.

<html>
<?php
/* This will give an error. Note the output
 * above, which is before the header() call */
header('Location: http://www.example.com/');
exit;
?>
Venkatesh K
  • 4,364
  • 4
  • 18
  • 26
0

"headers already sent error" means that your script (somepage.php) already sent headers to the browser, so you can't change these headers and redirect user to the other URL.

The best solution is to move the "include" operator after your check:

$id = $_GET['id'];
$query = mysql_query("My query is here");

if(mysql_num_rows($query)==0) { header('Location:http://example.com'); }

include('somepage.php');

Also, you can prevent your somepage.php from sending any data to the client.

The second variant is to use the functions ob_start(), ob_get_contents(), ob_end_clean():

ob_start();
include('somepage.php');

$id = $_GET['id'];
$query = mysql_query("My query is here");

if(mysql_num_rows($query)==0) { header('Location:http://example.com'); }
$content = ob_get_contents():
ob_end_clean();
echo $content;
edtech
  • 1,734
  • 20
  • 20