Jump to content

Cannot modify header information - headers already sent by (...).....


Recommended Posts

Why do i get this error when i put code for redirection on my php code :S

Warning: Cannot modify header information - headers already sent by (output started at /home/dotars/public_html/shop/includes/functions.php:3) in /home/dotars/public_html/shop/includes/functions.php on line 20

 

This is that code on functions.php on line 20:

function confirm_logged_in() {
	if (!logged_in()) {
		redirect_to("login.php");
	}
}
function redirect_to($location = NULL) {
	if ($location != NULL) {
		header("Location: {$location}");
		exit;
	}
}

 

And this is the code in the other php code:

<?php require_once("includes/session.php"); ?>
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php confirm_logged_in(); ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<center>
Text...
</center>
</body>
</html>

 

And confirm_logged_in() function is:

 

output started at /home/dotars/public_html/shop/includes/functions.php:3

 

^^^ Something on or up to line 3 in the functions.php file is sending output. You would need to find and eliminate that output.

The header() function cannot operate properly if anything has been sent to the browser.  As PFMaBiSmAd pointed out, line 3 of your functions file has output.  You must remove it in order for header() to work.

 

-Dan

Add this to your file before anything is written or included.

 

<?php
ob_start();
?>

 

What that code means?

 

On line 3 in functions.php file was now row, empty, i deleted it and it works, i dont know what was the problem :S

A "pro" php tip is to always omit the ending php tag from any script that only includes PHP code.  A line or 2 of whitespace can inadvertantly cause output to be triggered in scripts where you never intended that to happen -- like your functions.php include script.  Whenever a script is included, you have to realize that it is injected as is, into the including script.  Since PHP allows for the intermixing of PHP and HTML.  Leaving off the end tag will eliminate that problem.

 

 

 

 

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.