Jump to content

Cannot modify header information - headers already sent by


suttercain

Recommended Posts

Hi guys,

 

this is straight from a book I am working from:

<?php session_start(); ?>
<?php
require ('get_connected.php');
if (isset($_POST['user']) && isset($_POST['password'])) {
    $sql = "SELECT * FROM users";
    if ($result = mysql_query($sql)) {
      if (mysql_num_rows($result)) {
        $row = mysql_fetch_assoc($result);
		if ($user == $row['user'] && $password == $row['password'])	{
			$_SESSION['Approved'] = 1;
			} else {
			$_SESSION['Approved'] = 0;
			}
		header("Location: admin_canada.php");
	}
      }
    }

if (isset($_GET['logout'])) {
session_destroy();
header("Location: canada_login.php");
}
?>

 

Is this code incorrect? Becuase I am getting this error:

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\ARB\pio\get_connected.php:7) in C:\wamp\www\ARB\pio\logged_in.php on line 15

he doesn't have to post the included file. basically you've just printed something out to the browser before you called the header function. you can't do that.

http://www.phpfreaks.com/forums/index.php/topic,37442.0.html

 

the alternative would be to replace your header() with a meta-redirect:

http://webdesign.about.com/od/metataglibraries/a/aa080300a.htm

It all depends on what your intending to do with that include. Is it a form to login?

 

If so, you can use output buffering to make your current file work without errors, but there is a better way to do it than what your doing if that is whats in get_connected.php

 

use this as a last resort to make it work:

 

<?php 
session_start();
ob_start();
require ('get_connected.php');
$data=ob_get_contents();
ob_end_clean();
if (isset($_POST['user']) && isset($_POST['password'])) {
    $sql = "SELECT * FROM users";
    if ($result = mysql_query($sql)) {
      if (mysql_num_rows($result)) {
        $row = mysql_fetch_assoc($result);
		if ($user == $row['user'] && $password == $row['password'])	{
			$_SESSION['Approved'] = 1;
			} else {
			$_SESSION['Approved'] = 0;
			}
		header("Location: admin_canada.php");
	}
      }
    }

if (isset($_GET['logout'])) {
session_destroy();
header("Location: canada_login.php");
}
echo $data;
?>

docta dre is right about using output buffering. but i think that uses more resources than necessary and can cause problems to arise elsewhere in the future. meta-redirect does the exact same thing as header() for what you want to do.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.