Header function is not working in the host server

the header in my local server is working but in the hosted server is not!

header('Location: step3.php');

thank you!

Turn up error_reporting and read the error. It will likely tell you the problem.

this is the error

<b>Warning</b>: Cannot modify header information - headers already sent by (output started at /home3/nyhungry/public_html/includes/header.php:7) in <b>/home3/nyhungry/public_html/step2.php</b> on line <b>14</b><br />

Now do a google search on part of the error message to see others who have had the same error. This tactic works with most error messages. For example

Cannot modify header information - headers already sent

thank you malibu!

header.php


<!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" />
<link type="text/css" href="stylesheets/main.css" rel="stylesheet" media="all" /> 
<script type="text/javascript" src="scripts/prototype.js"></script>
<script type="text/javascript" src="scripts/rating.js"></script>
<!--[if IE 5]><link rel="stylesheet" type="text/css" href="stylesheets/ie-5.0+5.5.css" /><![endif]-->
<!--[if IE 6]><link rel="stylesheet" type="text/css" href="stylesheets/ie-6.0.css" /><![endif]-->
<!--[if IE 7]><link rel="stylesheet" type="text/css" href="stylesheets/ie-7.0.css"/><![endif]-->
<!--[if IE 8]><link rel="stylesheet" type="text/css" href="stylesheets/ie-8.0.css"/><![endif]-->
<title>www.lala.com</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
(
function()
{
  var default_image = $('td.largethumb img').attr('src');
  $('table.smallthumbs a').mouseover(function() { $('td.largethumb img').attr('src', $('img', this).attr('src')); });
});
</script>
</head>
<body>
<?php include_once("includes/usersOnline.class.php");?>
<?php require_once("includes/connection.php");?>
<?php require_once("classes/include.all.php");?>
<?php require_once("includes/functions.php");?>
<div id="sidebar1">
<h1><a href="index.php" title="nyhungry.com">ny.hungrylogo</a></h1>
<ul id="mainNav">
      <li class="home"><a href="index.php" title="All inside lala.com" class="first">Home</a></li>
                <li id="takeouts"><a href="step1.php" title="Information abouts">Contact Us</a></li>
             

step3.php

<?php include("includes/header.php");?>
<div id="tresdos"><?php
$name = $_POST['username'];
$email = $_POST['email'];
$text = $_POST['message'];
//TO, Subject, Message, Header
mail('mary@hotmail.com','Contact us Message',$text,'From:'.$name.'<'. $email.'>');
header("location: step3.php");?></div>
<?php mysql_close($connection);?>
<?phpDatabase::DeInitialize();?>
<?php include("includes/footer.php");?>

i have taken all the white space before and after php tags and still displaying this warning

<b>Warning</b>: Cannot modify header information - headers already sent by (output started at /home3/nyhungry/public_html/includes/header.php:6) in <b>/home3/nyhungry/public_html/step2.php</b> on line <b>8</b><br />
</div>
The file phpdatabase.php could not be found.

don’t get why

any help?

(output started at /home3/nyhungry/public_html/includes/header.php:6)

That means on line 6 in that file, you sent “output”. Html/text is output. Don’t send output.

In any case, if the goal is to redirect them, then sending html and stuff seems a bit pointless, because they wouldn’t see it.

it is true i should take the header.php out.

also good practice to exit the script when you redirect, as it will continue processing if you don’t (of course that could be intended behaviour).

I think exiting here would be good.

In the localhost it’s doing perfect no in the host server

Just add ob_start() at the beginning of your script, html output will be sent when your script is finished executing, that allows you to send headers whenever you want, at whatever line you want.


<?php ob_start(); ?>
<?php include("includes/header.php");?>
<div id="tresdos"><?php
$name = $_POST['username'];
$email = $_POST['email'];
$text = $_POST['message'];
//TO, Subject, Message, Header
mail('mary@hotmail.com','Contact us Message',$text,'From:'.$name.'<'. $email.'>');
header("location: step3.php");?></div>
<?php mysql_close($connection);?>
<?phpDatabase::DeInitialize();?>
<?php include("includes/footer.php");?>

Awww that’s surprising

I will record this fuction, I have taken all the includes of this file to avoid conflicting headers, But a future reference i might come across again with interfering headers.

thank you for that function.

There are some functions in PHP which don’t perfectly work if there is any output before the use of them like header, session_start, setcookie. So good practice would be to use them before any html output in the page/script. Or if you are compelled to use them in anyway then you can use buffering using ob_start, ob_end_flush. Use ob_start at the top of the script and you can flush when you dont need buffering.

Or you can use some JavaScript (document.location=‘redirecto.php’:wink: or meta refresh to redirect.

Can see there are so many techniques.

Javascript is very useful.

Now that you talk of javascript, I have a users online script where it will count how many users there are online. But it won’t refresh everytime a new user come in the website unless i click on the refresh bottom in the browser. I was wondering if it would be possible to have it refresh automatically without a browser refresh with javascript?

Yes that can be done with JavaScript.

See here:

I have posted a solution out there with AJAX.

This is not about the functions, but about the way http works. Those functions send headers, headers can’t be sent after the body. You can use OB if you really need, but if the logic is all done before the presentation you won’t have these problems.

Indeed hash! I just did not mention the logic behind it but I mentioned the functions that send the header.

in order for me to fix the header issue, I had to put the header before the html outpu and that was the way it worked. surprisingly it worked like a charmed. But now I can see there are some alternative options to go around even when you have a header after any other output.