Cannot Modify Header ERROR

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
khockenberry
Forum Newbie
Posts: 3
Joined: Tue Aug 02, 2011 3:33 pm

Cannot Modify Header ERROR

Post by khockenberry »

I am getting errors trying to redirect after login information is verified, I have used this same code on my school server with their database, and have had not 1 error while developing things their. However now I have used the same code on a go-daddy linux box and get errors. Any help is appreciated, I have already read about 8 forums all with what was thought to be useful but when tried did not help the problem what so ever.

khockenberry1734@gmail.com if any assistance could be provided

Checked for extra lines of code [white space]
Tried re arranging the code several different ways also.

ERRORS:
[text]1-Warning: Cannot modify header information - headers already sent by (output started at /home/content/50/8151650/html/TSP/Html/trial.php:6) in /home/content/50/8151650/html/TSP/Html/trial.php on line 66

2-Warning: Cannot modify header information - headers already sent by (output started at /home/content/50/8151650/html/TSP/Html/trial.php:6) in /home/content/50/8151650/html/TSP/Html/trial.php on line 67

3-Warning: session_register() [function.session-register]: Cannot send session cookie - headers already sent by (output started at /home/content/50/8151650/html/TSP/Html/trial.php:6) in /home/content/50/8151650/html/TSP/Html/trial.php on line 69

4-Warning: session_register() [function.session-register]: Cannot send session cache limiter - headers already sent (output started at /home/content/50/8151650/html/TSP/Html/trial.php:6) in /home/content/50/8151650/html/TSP/Html/trial.php on line 69

5-Warning: Cannot modify header information - headers already sent by (output started at /home/content/50/8151650/html/TSP/Html/trial.php:6) in /home/content/50/8151650/html/TSP/Html/trial.php on line 73[/text]

CODE:

Code: Select all

<!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>
<style type="text/css">
#TOP {
	width:100%;
	background-color:#5d085d;
	height:80px;
}
</style>
</head>
<body>
<?
$DEBUG = 1;
include("global2.inc.php");
//Connect To Database
HOSTNAME
USER
PASSWORD
// mysql_connect($hostname,$username, $password) or die ('Unable to connect to database! Please try again later.');
// mysql_select_db($dbname);
// If Logout Selected Unset User and Admin User Cookies
if(isset($_POST['Logout'])){
	setcookie('user',$user, time()-3600);
	setcookie('password',$pass, time()-3600);
	setcookie('adminuser',$user, time()-3600);
	setcookie('adminpassword',$pass, time()-3600);
}
if(isset($_COOKIE['user']) or ($_COOKIE['adminuser'])){
	header("location:loggedin2.php");
}
// If Login attempted
if(isset($_POST['Submit'])){
// Connect to server and select databse.
mysql_connect($hostname, $username, $password)or die("Cannot Connect"); 
mysql_select_db($dbname)or die("cannot select DB");
// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword'];
$user = $myusername;
$pass = $mypassword;
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE user='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
	if(isset($_COOKIE['user'])){
		setcookie('user',$user, time()-3600);
		setcookie('password',$pass, time()-3600);
	}
	if(isset($_COOKIE['adminuser'])){
		setcookie('adminuser',$user, time()-3600);
		setcookie('adminpassword',$pass, time()-3600);
	} 
	setcookie('user',$user);
	setcookie('password',$pass);
	// Register $myusername, $mypassword and redirect to file "login_success.php"
	session_register("myusername");
	session_register("mypassword");
	?>
    <input type="hidden" name="user" value="<? print $myusername ?>"/>
	<? header("location:loggedin2.php");
	}
}
?>
<div id="TOP">
<font color="#000000">
<form name="form1" method="post" action="trial.php">
	<font color="#FFFFFF">
    <table>
	<Tr><Td rowspan="2"><h1>Kyle Hockenberry's Site</h1></Td>
        <td style="padding-left:500px;" valign="bottom">UserName</td>
        <td valign="bottom">Password</td>
    </Tr><tr>
    	<td style="padding-left:500px;"><input name="myusername" type="text" id="myusername"></td>
        <td><input name="mypassword" type="password" id="mypassword"></td>
        <td><input type="submit" name="Submit" value="Login" /></td><td><? if(isset($_POST['Submit'])){
	 	echo ('<font color="#FFFFFF" size="-1"><i>* Wrong Username or Password</i></font>'); 
	 } 
	 ?></Td></tr>
        </table>
	</font>
</form>
</font>
</div>
</body>
</html>

Thanks!
Last edited by Benjamin on Thu Aug 04, 2011 2:50 am, edited 1 time in total.
Reason: Added [syntax=php|sql|css|javascript] and/or [text] tags.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Cannot Modify Header ERROR

Post by twinedev »

To use the header() function no output can already have been sent to the browser (output).

On your code, you already output several lines of HTML code before it even gets into PHP code (which is a good reason to put all processing before output).

The quick/sloppy way to handle this, add <?php ob_start() ?> as the first line in your code. This will tell PHP to start buffering output (ie don't actually send output until script finishes or specifically told to do so). My guess would be that the environment you were running on before was set to auto buffer output by default.

The proper way to handle this would be to re-write the code so all logic is done before any output.

-Greg
khockenberry
Forum Newbie
Posts: 3
Joined: Tue Aug 02, 2011 3:33 pm

Re: Cannot Modify Header ERROR

Post by khockenberry »

Greg,

I added the ob_start() at the beginning and still no luck at all. Are there any other solutions? Or any advice for how it should be re-organized? I really appreciate the time your taking to help me.

-Kyle
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Cannot Modify Header ERROR

Post by pickle »

It says clearly that "output started at /home/content/50/8151650/html/TSP/Html/trial.php:6" Check that line in that file. Also please post you code in proper [syntax] tags so it gets highlighted properly & doesn't take over the screen.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Dodon
Forum Commoner
Posts: 64
Joined: Wed Aug 03, 2011 4:11 am
Location: Netherlands

Re: Cannot Modify Header ERROR

Post by Dodon »

Like Greg said, in order to use Header("Location: ..... ") you can't have any html tag or any other kind of text outputted to the webpage.

Code: Select all

<!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>
<style type="text/css">
#TOP {
 width:100%;
 background-color:#5d085d;
 height:80px;
}
</style>
</head>
<body>
Above code should not be placed before doing a header("location: ... "). Read the php.net info http://se.php.net/manual/en/function.header.php

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
Post Reply