"...Remember being a youngster, mom or dad telling you "close the door"? Well the same applies to HTML ...."

Go Back   Webmaster Forums > Code Forum > PHP Forum

Reply
 
Thread Tools Rate this Webmaster Discussion
Old 05-24-2009, 06:55 AM   #1
zzz
New Member
 
Join Date: Jan 2009
Webmaster Discussions: 5
Rep Power: 11
zzz is on a distinguished road
Warning: Cannot modify header information - headers already sent by (output started a

hi,
can anybody help me i this regards.
problem is as follows

i have a form .
here i am getting a value through a file.
based on the value i decide the header.but before that i pass a meta data also.

<html>
<meta http-equiv="refresh" content="2;url=http://localhost/cba.php" />
<?php
$handle=fopen('c:/aaa.txt','r');
$a=fgets($handle);
fclose($handle);
if($a!=0)
header( 'refresh: 5; url=http://localhost/abc.php' );
else
header( 'refresh: 5; url=http://localhost/cba.php' );
?>
</html>

so here once i do this a warning will come up saying
"Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\abc.php:7) in C:\Program Files\Apache Group\Apache2\htdocs\abc.php on line 462
"

so can anyone give me solution for this.pls


zzz is offline   Reply With Quote
Old 05-29-2009, 12:46 PM   #2
jthayne
 
jthayne's Avatar
 
Join Date: Aug 2008
Location: Texas
Webmaster Discussions: 501
Rep Power: 21
jthayne is the hardest working person in the biz.jthayne is the hardest working person in the biz.jthayne is the hardest working person in the biz.jthayne is the hardest working person in the biz.
Re: Warning: Cannot modify header information - headers already sent by (output start

Sorry about the delayed response. I have been gone for a little while, but I am back now.

When using the header() function in PHP, there can be nothing output to the screen before the function is called. Any HTML is considered output to the screen. What you need to do is move your PHP code to before the HTML as follows:

PHP Code:
<?php
$handle
=fopen('c:/aaa.txt','r');
$a=fgets($handle);
fclose($handle);
if(
$a!=0)
header'refresh: 5; url=http://localhost/abc.php' );
else
header'refresh: 5; url=http://localhost/cba.php' );
?>
<html>
<meta http-equiv="refresh" content="2;url=http://localhost/cba.php" />
</html>
Viola! No more error.
__________________
Be sure to click the reputation icon to give rep to the person who helped you.
For web design/development services, check Silentium Designs.

Last edited by jthayne; 05-29-2009 at 12:53 PM.
jthayne is offline   Reply With Quote
Old 08-26-2009, 12:19 AM   #3
markei
New Member
 
Join Date: Mar 2009
Location: Westminster, MD
Webmaster Discussions: 19
Rep Power: 10
markei has a spectacular aura aboutmarkei has a spectacular aura about
Wink Re: Warning: Cannot modify header information - headers already sent by (output start

Viola? Why not Bass Fiddle?
markei is offline   Reply With Quote
Old 11-02-2009, 03:44 AM   #4
kaly
New Member
 
Join Date: Nov 2009
Webmaster Discussions: 1
Rep Power: 6
kaly is on a distinguished road
Re: Warning: Cannot modify header information - headers already sent by (output start

hi,
i m getting the same "Cannot modify header information - headers already sent in .. " error. There's no white space b4 or after php tags. Here is my code-

<? ob_start(); ?>
<?php
session_start();
if(isset($_POST['login']))
{
if(!$fh = fopen('pwd.dat','w')) {
do_error_exit('Error opening data file. <br />Please contact the system administrator. <br />Sorry for the inconvenience.');
}

$username1[0]= 'kaly';
$username1[1]= crypt('mypassword','rl');

$user_pass_string= implode('=',$username1);
fwrite($fh,"$user_pass_string\n");

flock($fh);
flush($fh);
fclose($fh);

$username = $_POST['username'];
$password = $_POST['pswd'];

$tempwd = crypt($password,'rl');

$fh = fopen('pwd.dat','r');

for($line = fgets($fh); !feof($fh) ; $line = fgets($fh))
{
$line = trim($line);
$userData = explode("=",$line);
if ($userData[0] == $username)
$pass = $userData[1];

}
fclose($fh);
if (crypt($password, $pass) == $pass){
$_SESSION['phplogin'] = true;
header('Location: index.php');
exit;
} else {
?>
<script type="text/javascript">
<!--
alert('Wrong Password, Please Try Again')
//-->
</script>
<?php
}
}
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> PHP Login </title>
</head>
<body>
<center>
Username:<br>
Password:<br>
<form method="post" action="">
<input type="username" name="username">
<input type="password" name="pswd">
<input type="submit" name="login" value="Login">
</form>
</center>
</body>
</html>
<? ob_flush(); ?>
kaly is offline   Reply With Quote
Old 11-02-2009, 01:23 PM   #5
sac0o01
New Member
 
sac0o01's Avatar
 
Join Date: Nov 2009
Location: USA
Webmaster Discussions: 1
Rep Power: 6
sac0o01 is on a distinguished road
Re: Warning: Cannot modify header information - headers already sent by (output start

I would say Short Tags are not enabled on your host. You can change it in your php.ini file but it is easiest to fix the code.

Change:
PHP Code:
<? ob_start(); ?>
To:
PHP Code:
<?php ob_start(); ?>
Remember to change the line at the bottom as well.
sac0o01 is offline   Reply With Quote
Old 11-04-2009, 10:11 AM   #6
jthayne
 
jthayne's Avatar
 
Join Date: Aug 2008
Location: Texas
Webmaster Discussions: 501
Rep Power: 21
jthayne is the hardest working person in the biz.jthayne is the hardest working person in the biz.jthayne is the hardest working person in the biz.jthayne is the hardest working person in the biz.
Re: Warning: Cannot modify header information - headers already sent by (output start

Actually what needs to happen, is you need to move
PHP Code:
<? ob_start(); ?>
to just before you actually have output you are buffering as this function opens the output buffer which is where your problem lies.

My recommendation is to move it just before this line:
HTML Code:
<script type="text/javascript">
__________________
Be sure to click the reputation icon to give rep to the person who helped you.
For web design/development services, check Silentium Designs.
jthayne is offline   Reply With Quote
Reply

Bookmarks

Tags
header, modify

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Webmaster Discussions
Thread Webmaster Discussion Starter Forum Replies Last Post
mypsace modify friends help? ineedhelpmyspace Myspace Forum 0 06-19-2009 12:17 PM
Premature end of script headers error madan9999 CGI Perl Forum 1 08-16-2006 02:16 PM
Question about Headers (I think) sshadow HTML Forum 5 04-10-2001 02:48 PM


All times are GMT -5. The time now is 08:53 PM.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.