Redirect warning

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
scorp54
Forum Newbie
Posts: 3
Joined: Thu Apr 29, 2010 11:55 am

Redirect warning

Post by scorp54 »

Howdo,
I've been searching everywhere and trying everything so far to no avail.
This is the error message I'm getting while logging in:
Warning: Cannot modify header information - headers already sent by (output started at /home/param/public_html/edit/includes/header.php:8) in /home/param/public_html/edit/includes/functions.php on line 33.
I can hit refresh on the browser and then resent and everything works fine from that point on. Also the code works flawlessly on my local drive.

This is the code area in question for header.php, there actually is no php here so I don't understand why the warning is pointing here.

Code: Select all

6. <link href="styles/public.css" media="all" rel="stylesheet" type="text/css" />
7. </head>
8. <body bgcolor="#cfcfcf">
9. <table width="1000" border="0" cellspacing="0" cellpadding="0" align="center">
Code for functions.php. I have no trouble going to other pages once I'm in.

Code: Select all

31.	function redirect_to( $location = NULL ) {
32.		if ($location != NULL) {
33.			header("Location: {$location}");
34.			exit();
35.		}
36.	}
Just for good measure, the session code which is called from every page.

Code: Select all

<?php require_once("includes/functions.php"); ?>
<?php 
	session_start();
	
	function logged_in() {
		return isset($_SESSION['user_id']);
	}
	
	function confirm_logged_in() {
		if (!logged_in()) {
			redirect_to("login.php");
		}
	}
?>
Things I have done:
Made sure the session call is the first thing on the pages.
Made sure there is no white space before the session calls.
Copied the code from Dreamweaver, pasted it into Notepad and save as ANSI.
Pulled out a lot of hair. :banghead:
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Redirect warning

Post by Christopher »

You are outputting a carriage return between the two PHP blocks. ANY output will cause header to be sent.

Code: Select all

<?php require_once("includes/functions.php"); ?>
<?php 
(#10850)
User avatar
scorp54
Forum Newbie
Posts: 3
Joined: Thu Apr 29, 2010 11:55 am

Re: Redirect warning

Post by scorp54 »

Sorry, that seems to make no difference unless putting it together like below isn't what you're getting at.

Code: Select all

<?php 
	require_once("includes/functions.php");
	session_start();
Thanks for the replay.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Redirect warning

Post by Christopher »

Either that:

Code: Select all

<?php 
	require_once("includes/functions.php");
	session_start();
Or this:

Code: Select all

<?php 
	require_once("includes/functions.php");
?><?php
	session_start();
There just can be ANY output -- even whitespace -- before you call functions that send headers like header() or session_start().
(#10850)
User avatar
scorp54
Forum Newbie
Posts: 3
Joined: Thu Apr 29, 2010 11:55 am

Re: Redirect warning

Post by scorp54 »

Yea, that didn't work but I found a cheat that took care of the problem.
Put a php.ini file in the offending area with "output_buffering = On" written in it.
Thanks for your help though.
Post Reply