Jump to content

Headers already sent error, even with ob_start();?


oni-kun

Recommended Posts

I a long time ago made a simple index for my site that includes the file via $_GET and a switch statement. This one script I have needs it to not be included for certain reasons but redirected. But it's not working..

 

Is having it nested, or having break; after it the problem?..

 

"Warning: Cannot modify header information - headers already sent by (output started at /xxxxx/public_html/index.php:1) in /xxxxx/public_html/index.php on line 54"

 

ob_start is at the first position and my code is like this.. (the header() is near the bottom)

<?php 
ob_start();
// Navi system.
include('./logger.php'); //simple IP logging
$server = $_SERVER['SERVER_NAME'];
if (isset($_GET['page'])) {
$page = $_GET['page'];
if ($page == "") {
	require('http://'.$server.'/front.php');
} else {
switch($page)
{
case 'home';
	require('http://'.$server.'/front.php');
	break;
case 'contactus';
	require('http://'.$server.'/contactus.php');
	break;
case 'games';
	require('http://'.$server.'/games/games.php');
	break;
case 'downloadgames';
	require('http://'.$server.'/games/downloadgames.php');
	break;
default;
	require('http://'.$server.'/404.php');
	break;
}	
}
} else {
if (isset($_GET['page']) && !isset($_GET['project'])) {
	include('http://'.$server.'/front.php');
} elseif (!isset($_GET['page']) && !isset($_GET['project'])) {
	//if neither are set
	include('http://'.$server.'/front.php');
}
}

//proj urls
if (isset($_GET['project'])) {
$page = $_GET['project'];
if ($page == "") {
	require('http://'.$server.'/front.php');
} else {
switch($page)
{
case 'translate';
	require('http://'.$server.'/projects/translator.php');
	break;
case 'identify';
	require('http://'.$server.'/projects/detection.php');
	break;
case 'trace';
	header('Location: http://'.$server.'/projects/trace.php'); //The line with the error..
	break;
case 'whois';
	require('http://'.$server.'/projects/whois_i.php');
	break;
default;
	require('http://'.$server.'/404.php');
	break;
}	
}
} else {
if (!isset($_GET['page']) && isset($_GET['project'])) {
	include('http://'.$server.'/front.php');
} else {
	//donothing, pass to proj.
}
}
?>

Link to comment
Share on other sites

you probably have a white space or special character before your ob_start(); check your file format ?

 

I double checked.

<?php ob_start(); ... 

At absolute beginning doesn't change it. My site went down to PHP 4.3 for some stupid reason maybe that is why.. Any other thoughts?

Link to comment
Share on other sites

ob_start() should only be used if you intentionally want to buffer output. It should not be used to fix header errors. In your case it did not help did it? And frankly, we are tired of people attempting to use it to 'fix' header errors instead of finding the problem with their code and fixing their code.

 

Your problem is that your file either contains characters in it before the opening <?php tag (in which case just check if there are any characters before the <?php tag and remove them) or your file has been saved as a UTF-8 encoded file and your editor has put the BOM (Byte Order Mark) characters at the start of the file (in which case, save your file as an ANSI encode file or save it as a UTF-8 encoded file without the BOM.)

Link to comment
Share on other sites

ob_start() should only be used if you intentionally want to buffer output. It should not be used to fix header errors. In your case it did not help did it? And frankly, we are tired of people attempting to use it to 'fix' header errors instead of finding the problem with their code and fixing their code.

 

Your problem is that your file either contains characters in it before the opening <?php tag (in which case just check if there are any characters before the <?php tag and remove them) or your file has been saved as a UTF-8 encoded file and your editor has put the BOM (Byte Order Mark) characters at the start of the file (in which case, save your file as an ANSI encode file or save it as a UTF-8 encoded file without the BOM.)

 

Oh man I never thought of that! I used a hex editor and found the BOM, I never put two and two together. Safe to say after deleting it, it worked!

 

I think people use headers alot because they're not sure how to redirect? I'm sure not sure how to redirect to another page with PHP.. if it really is possible without output buffering.. Does the job.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.