Jump to content

Recommended Posts

Sorry to ask such a "newb" question, but I'm a bit fuzzy on this...

 

When you get a "Headers Already Sent" error, can that be caused by spaces between your HTML??  :confused:

 

I am having trouble getting my Log-In script to work, and I finally broke down and added Output Buffering to get things working, although a couple of senior members here scolded me in the past as basically saying it was slopping coding to need Output Buffering?!  :shy:

 

Here is my problematic code...

<?php
// Initialize a session.
session_start();

// Access Constants.
require_once('config/config.inc.php');
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<!-- HTML Metadata -->
<title>My Site - Log In</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description"
			content="Log in to your account to access special member-only areas.">
<meta name="keywords"
			content="">

<!-- Page Stylesheets -->
<link type="text/css" rel="stylesheet" href="css/main.css" />
<link type="text/css" rel="stylesheet" href="css/dropdown.css" />
<link type="text/css" rel="stylesheet" href="css/log_in.css" />
</head>

<body>
<div id="wrapper" class="clearfix">
	<div id="inner">
		<?php

 

I have session_start() at the very beginning, and no spaces between my opening PHP and HTML, so the only thing I can figure is the carriage returns within my HTML?!

 

Thanks,

 

 

Debbie

 

P.S.  Is using Output Buffering really that evil??

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/245479-clarification-on-headers-already-sent/
Share on other sites

Make sure there is nothing, not even a single whitespace character before your opening PHP tag. It must be at the very top.

 

On output buffering, no it's not evil and yes it does have its uses. I've used it to capture the output of included view files so I can manipulate the views or pass variables to them etc. Many frameworks do the same thing. But for something like the old "headers already sent" error you should avoid using output buffering to patch it up. If you need to send headers or start a session it should always be done at the very very top before absolutely anything.

Make sure there is nothing, not even a single whitespace character before your opening PHP tag. It must be at the very top.

 

On output buffering, no it's not evil and yes it does have its uses. I've used it to capture the output of included view files so I can manipulate the views or pass variables to them etc. Many frameworks do the same thing. But for something like the old "headers already sent" error you should avoid using output buffering to patch it up. If you need to send headers or start a session it should always be done at the very very top before absolutely anything.

 

So blank lines in between lines in my HTML won't cause that error?

 

 

Debbie

 

 

Make sure there is nothing, not even a single whitespace character before your opening PHP tag. It must be at the very top.

 

See my code above.

 

No spaces that I can see.

 

And here is my included Config file...

 

<?php
define('ENVIRONMENT', 'development');
//define('ENVIRONMENT', 'production');

// File Root
define('ROOT', ENVIRONMENT === 'development'
				? '/Users/user1/Documents/DEV/++htdocs/01_MyProject/'
				: '/var/www/vhosts/MyWebsite.com/httpdocs/');

// Web Server Root
define('WEB_ROOT', ENVIRONMENT === 'development'
				? 'http://local.dev/'
				: 'http://www.MyWebsite.com/');

// Secure Web Server Root
define('SECURE_WEB_ROOT', ENVIRONMENT === 'development'
				? 'http://local.dev/'
				: 'https://www.MyWebsite.com/');
?><? //Build Date: 2011-08-14 10:13am ?>

 

So what gives?!  :shrug:

 

 

Debbie

 

 

nevermind Voip linked it :P

 

edit: ps, you using short tags on this:

<? //Build Date: 2011-08-14 10:13am ?>

Depending on the settings that will or will not be executed and just be output as is. Don't use short tags unless you did it on purpose.

 

I am running into issues with this code...

 

// Redirect User.
if (isset($_SESSION['returnToPage'])){
header("Location: " . $_SESSION['returnToPage']);
}else{
// Take user to Home Page.
header("Location: " . WEB_ROOT . "index.php");
}

 

 

I can't exactly put that at the top of my file...

 

And I need HTML before that to display my Log-In form.

 

I thought only things output to the screen caused this issue?

 

So spaces in my PHP code and spaces in between my HTML shouldn't matter, right?

 

Only spaces before my HTML or spaces in things like PHP echoes should cause the issue.

 

Can someone please look at my code snippets again, please?    :-\

 

Thanks,

 

 

 

Debbie

 

 

nevermind Voip linked it :P

 

I read the link and it doesn't help me figure out my issue...

 

 

edit: ps, you using short tags on this:

<? //Build Date: 2011-08-14 10:13am ?>

Depending on the settings that will or will not be executed and just be output as is. Don't use short tags unless you did it on purpose.

 

Okay, I fixed it.

 

But I still need help sleuthing my Headers issue...

 

 

Debbie

 

 

make a template for any code you write and above everything put:

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);


// rest of the code

?>

 

anytime you get an error. Give us the precise error message (they are detailed). If it refers to a line. Give us the code that is on and around that line and tell us what the line is.

 

In a Nutshel the code you use or the file you have is already outputting stuff to the browser. quite often this is just whitespace. If you can attach your Complete file it would be easier to spot, because now it's just guessing.

make a template for any code you write and above everything put:

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);


// rest of the code

?>

 

No need, that is why I use NetBeans!!  ;)

 

 

Warning: Cannot modify header information - headers already sent by (output started at /Users/user1/Documents/DEV/++htdocs/01_MyProject/log_in.php:20) in /Users/user1/Documents/DEV/++htdocs/01_MyProject/log_in.php on line 112

 

 

log_in.php (Lines 1-27)

<?php
// Start Output Buffering.
//	ob_start();

// Initialize a session.
session_start();

// Access Constants.
require_once('config/config.inc.php');
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<!-- HTML Metadata -->
<title>My Site - Log In</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description"
			content="Log in to your account to access special member-only areas." />
<meta name="keywords"
			content="Something" />

<!-- Page Stylesheets -->
<link type="text/css" rel="stylesheet" href="css/main.css" />
<link type="text/css" rel="stylesheet" href="css/dropdown.css" />
<link type="text/css" rel="stylesheet" href="css/log_in.css" />
</head>

 

 

log_in.php (Lines 110-116)

// Redirect User.
if (isset($_SESSION['returnToPage'])){
header("Location: " . $_SESSION['returnToPage']);
}else{
// Take user to Home Page.
header("Location: " . WEB_ROOT . "index.php");
}

 

 

 

Debbie

 

 

And there you have it!!

 

at line 112 your trying to do a header() which you can't because you already sent stuff (pretty much your whole page)

 

// Redirect User.
if (isset($_SESSION['returnToPage'])){
header("Location: " . $_SESSION['returnToPage']);
}else{
// Take user to Home Page.
header("Location: " . WEB_ROOT . "index.php");
}

 

Place this In the top part of your code above <doctype> and it should be fine.

 

And this is why we like precise error reports

And there you have it!!

 

at line 112 your trying to do a header() which you can't because you already sent stuff

 

// Redirect User.
if (isset($_SESSION['returnToPage'])){
header("Location: " . $_SESSION['returnToPage']);
}else{
// Take user to Home Page.
header("Location: " . WEB_ROOT . "index.php");
}

 

Place this In the top part of your code above <doctype> and it should be fine.

 

And this is why we like precise error reports

 

Where did I send the Header above line 112?

 

And, no, I can't move this line.  The reason it is on line 112, is because I have to run my code and look for the user before I can determine where to route them.

 

 

Debbie

 

P.S.  Sorry I can't share my entire file...

 

 

well as already mentioned by pretyy much everyone here and the link that was given even a slight whitespace will cause headers sent (Stuff is sent to your browsers, text, whitespace monkeys images whatever)

 

If you can't place this code above, I would revisit your code to be honest. IF you don't feel like it, have a look again at that link givven and play around with ob_start().

 

anyway the cause of this error is now clear and you know how to solv it now. place this stuff before anything is being output (sent to the browser) or play around with ob_start()

well as already mentioned by pretyy much everyone here and the link that was given even a slight whitespace will cause headers sent (Stuff is sent to your browsers, text, whitespace monkeys images whatever)

 

If you can't place this code above, I would revisit your code to be honest. IF you don't feel like it, have a look again at that link givven and play around with ob_start().

 

anyway the cause of this error is now clear and you know how to solv it now. place this stuff before anything is being output (sent to the browser) or play around with ob_start()

 

Okay, but you can't identify WHAT is being set and FROM WHERE more importantly??

 

(I don't see anything being sent on the error line that it claims is the issue...)

 

 

Debbie

 

 

hmm...

 

Look if you rightclick your page and press view source. Anything you see there is sent to your browser. If your code with the header() is inside there (you won't see it since it is php serverside) you will get Headers already sent.

 

SO your code with using header() MUST come Before all the stuff you see when you pressed view source.

 

So to repeat it. if you right click and than do view source. ALL that stuff is sent to your browser and thus the headers are sent. If you have header() inside of that code (although you wont see it since it is php serverside code) It won't execute and throw an error.

so to do it properly

 

<?php  header( some stuff); // above EVERYTHING!!!!!!!!!!!!!!!!!!!!!!!! ?>
<!DOCTYPE html
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>PAGE A</title>
    </head>
    <body>
...etcetera

p.s. I am off to work

Let's look at your code Debbie. This is the start of your log_in.php file. As you can see you start displaying HTML. That's all good.

 

<?php
// Start Output Buffering.
//	ob_start();

// Initialize a session.
session_start();

// Access Constants.
require_once('config/config.inc.php');
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<!-- HTML Metadata -->
<title>My Site - Log In</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description"
			content="Log in to your account to access special member-only areas." />
<meta name="keywords"
			content="Something" />

<!-- Page Stylesheets -->
<link type="text/css" rel="stylesheet" href="css/main.css" />
<link type="text/css" rel="stylesheet" href="css/dropdown.css" />
<link type="text/css" rel="stylesheet" href="css/log_in.css" />
</head>

 

But now further down the file you're trying to use header. That's a no no. You've already begun to show HTML above it, so it chucks a tantrum and throws errors at you. Bummer huh. Basically, to fix your problem you must do any sending of headers or starting sessions before you output any HTML. So technically this stuff would need to above everything.

// Redirect User.
if (isset($_SESSION['returnToPage'])){
header("Location: " . $_SESSION['returnToPage']);
}else{
// Take user to Home Page.
header("Location: " . WEB_ROOT . "index.php");
}

 

If you can't do that, then the fact is that you probably aren't piecing it together very well. So that leaves us with output buffering. What you're doing with output buffering is preventing any of your HTML from being sent. The stuff that you display is instead stored internally. This allows you to still send headers and start sessions. Then at the end of the page you flush the internal buffer thus displaying all of the contents of the page. Seems pretty good. It is. But in your case you shouldn't really be doing it. But if you really can't re-structure your code then you may have to settle for buffering your output.

 

Good luck.

Output Buffering is a solution to this problem.

 

In my opinion, it's not the right one. You need to change the logic of your code to allow you to output headers before you output HTML.

 

Do you really need to echo your HTML before you check if you need to redirect? Why? The HTML you echo should never be seen if you want to redirect.

 

Keeping header calls at the top makes debugging much easier. Nothing is more painful than having to hunt down a redirect that SHOULD be at/near the top of the file.

Output Buffering is a solution to this problem.

 

In my opinion, it's not the right one. You need to change the logic of your code to allow you to output headers before you output HTML.

 

Do you really need to echo your HTML before you check if you need to redirect? Why? The HTML you echo should never be seen if you want to redirect.

 

Keeping header calls at the top makes debugging much easier. Nothing is more painful than having to hunt down a redirect that SHOULD be at/near the top of the file.

 

May I cry now??

 

I took all of my PHP code - or 95% of it - and put it at the top of my file last night.  (Following what you said today.)

 

Unfortunately, now things are really broken, and it is hard for me to post code here because...

 

1.) I don't want to show all of my website and code

 

2.) There are a million files and no easy way to do that.

 

I'm trying to figure out how to explain the NEW problem here...  *SIGH*

 

 

Debbie

 

The process that you're going through is what will turn you from an amateur to someone with at least a modest idea of what they're doing.  All of us had to go through it.  It's a painful process, but you'll be thankful you went through it.

 

A lot of beginners think the best way to code is to jump in and out of PHP/HTML whenever they want.  The language has that option as a feature, so it must be the way to go, right?

 

Wrong.

 

That's the classic trap of PHP.  Well designed sites do ALL of their data processing before they attempt to display anything to the screen.  Why?  For a multitude of reasons:

 

To avoid header problems like you're currently experiencing.

To react better to the HTTP request/response cycle.

To make the code itself far tidier, which, in turn, makes maintenance far simpler.

 

Unfortunately, a lot of tutorials available online are (very) dated, and show exactly the wrong way to write scripts, so newbies learn the wrong way to approach the problem.  Even worse, many think they're on the right track when they're actually writing badly designed sites.

 

Just remember: process then display.

The process that you're going through is what will turn you from an amateur to someone with at least a modest idea of what they're doing.  All of us had to go through it.  It's a painful process, but you'll be thankful you went through it.

 

A lot of beginners think the best way to code is to jump in and out of PHP/HTML whenever they want.  The language has that option as a feature, so it must be the way to go, right?

 

Wrong.

 

That's the classic trap of PHP.  Well designed sites do ALL of their data processing before they attempt to display anything to the screen.  Why?  For a multitude of reasons:

 

To avoid header problems like you're currently experiencing.

To react better to the HTTP request/response cycle.

To make the code itself far tidier, which, in turn, makes maintenance far simpler.

 

Unfortunately, a lot of tutorials available online are (very) dated, and show exactly the wrong way to write scripts, so newbies learn the wrong way to approach the problem.  Even worse, many think they're on the right track when they're actually writing badly designed sites.

 

Just remember: process then display.

 

All sage advice, I'm sure, but not any less frustrating?!

 

(50% of my problem is that it s a real pain-in-the-*ss to try debugging code on a screen that is maybe 9 inches by 13 inches...)

 

Drink lots of coffee tonight, guys, I will need some help soon!

 

 

Debbie

 

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.