Php error issues

Hi there,
I have been working on a web site and at a certain stage, I got the following errors while trying to upload images:

Warning: Cannot modify header information - headers already sent by (output started at /Library/WebServer/Documents/projectAD/includes/header.html:8) in /Library/WebServer/Documents/projectAD/loggedin.php on line 84

now here is loggedin.php file:
<?php # loggedin.php #2

// The designer is redirected here from login.php.

session_start(); // Start the session.
// If no session value is present, redirect the user:
if (!isset($_SESSION[‘user_id’])) {
require_once (‘loginFunctions.inc.php’);
$url = absolute_url();
header("location: ".$url);
exit();
}
$page_title = ‘Welcome to ADesigns: Logged In!’;
include (‘includes/header.html’);
// Print a customized message:
echo “<h1>Logged In!</h1> <p>You are now logged in, {$_SESSION[‘firstname’]}!</p>”;

// Now we upload, view and delete photos here:
require_once (‘mysql_connect.php’);
if ( isset($_GET[‘action’]) )
{
$action = $_GET[‘action’];
}
else
{
$action = “”;
}

if ( ($action == 'view' or $action == 'dnld') and isset($_GET['id']) )	
{
		$id = $_GET['id'];
	
		//	User is retreving a file
		$sql = "SELECT filename, mimetype, filedata FROM images WHERE id = '$id' ";
		$result = @mysql_query($sql);
	
		if (!$result)	
		{
			exit('Database error: ' . mysql_error());
		}
	
		$file = mysql_fetch_array($result);
	
		if(!$file)	
		{
			exit('File with given ID not found in database!');
		}
	
		$filename     = $file['filename'];
		$mimetype     = $file['mimetype'];
		$filedata     = $file['filedata'];
		$disposition  = 'inline';
	
		if ($action == 'dnld')	
		{
			$disposition  = 'attachment';			
			if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 5') or strpos($_SERVER['HTTP_USER_AGENT'], 'Opera 7') ) 	
			{
				$mimetype = 'application/x-download';
			}
		}
	
		header("content-disposition: $disposition; filename=$filename");
		header("content-type: $mimetype");
		header('content-lenght: ' . strlen($filedata));
	
		echo ($filedata);
		exit();
}

else if($action == 'del' and isset($_GET['id'])) 
{
		$id = $_GET['id'];
	
		//	User deleting a file
	
		$sql = "DELETE FROM images WHERE id = '$id'";	
		$ok = @mysql_query($sql);
		if (!$ok)	
		{
			exit('Database error: ' . mysql_error());
		}
	
		header('location: ' . $_SERVER['PHP_SELF']);
		exit();
}

else if (isset($_FILES['upload']) )	
{

		// 	Bail out if the file isn't really an upload.
	
		if (!is_uploaded_file($_FILES['upload']['tmp_name']))	
		{
			exit('There was no file uploaded!');
		}
	
		$uploadfile		=	$_FILES['upload']['tmp_name'];
		$uploadname		=	$_FILES['upload']['name'];
		$uploadtype		=	$_FILES['upload']['type'];
		$uploaddesc		=	$_POST['desc'];
		
		//	Open file or binary reading ('rb')
		$tempfile = fopen($uploadfile, 'rb');
	
	
		//	Read the entire file into memory using PHP's
		//	filesize fucntion to get the size.
		$filedata = fread($tempfile, filesize($uploadfile));
	
		//	Prepare for database insert by adding backslashes
		//	before special characters.
		$filedata = addslashes($filedata);
	
	
		//	Create the SQL query.
		$sql = "INSERT INTO images SET 
				filename = '$uploadname', 
				mimetype = '$uploadtype',
				description = '$uploaddesc',
				filedata = '$filedata'";
			
		//	Perform the insert.
		$ok = @mysql_query($sql);
	
		if(!$ok)	
		{
			exit('Database error storing file: ' . mysql_error());
		}
	
		header('location: ' . $_SERVER['PHP_SELF']);
		exit();
	
}


//	Default page view: lists stored files

$sql = 'SELECT id, filename, mimetype, description FROM images';
$filelist = @mysql_query($sql);

if (!$filelist)	
{
	exit('Database error: ' . mysql_error());
}

?>

	&lt;h1&gt;Upload your images:&lt;/h1&gt;	
	&lt;form action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;"	method="post" enctype="multipart/form-data"&gt;
		&lt;p&gt;&lt;label&gt;Upload File:&lt;br /&gt; 
			&lt;input type="file" name="upload" /&gt;&lt;/label&gt;&lt;/p&gt;
		
		&lt;p&gt;&lt;label&gt;File Description:&lt;br /&gt; 
			&lt;input type="text" name="desc" maxlength="255" /&gt;&lt;/label&gt;&lt;/p&gt; 
		
		&lt;p&gt;&lt;input type="submit"	value="Upload" /&gt;&lt;/p&gt;
	
	&lt;/form&gt;
	
	
	&lt;p&gt;The following are stored in the database:&lt;/p&gt;
	
	&lt;table&gt;
			&lt;tr&gt;
				&lt;th colspan="2"&gt;Filename&lt;/th&gt;
				
				&lt;th&gt;Type&lt;/th&gt;
				
				&lt;th&gt;Description&lt;/th&gt;
			&lt;/tr&gt;

<?php

if (mysql_num_rows($filelist) &gt; 0)	
{
	while ($f = mysql_fetch_array($filelist))	
	{

?>

			&lt;tr valign="top"&gt;
				
				&lt;td&gt;
						&lt;a href="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;?action=view&id=&lt;?php echo $f['id']; ?&gt;"&gt;
							&lt;?php echo $f['filename']; ?&gt;&lt;/a&gt;
				&lt;/td&gt;
				
				&lt;td&gt;&lt;?php echo $f['mimetype']; ?&gt;&lt;/td&gt;
				
				&lt;td&gt;&lt;?php echo $f['description']; ?&gt;&lt;/td&gt;
				
				&lt;td&gt; 	
					[&lt;a href="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;?action=dnld&id=&lt;?php echo $f['id']; ?&gt;"&gt;Download&lt;/a&gt;	|
							
					&lt;a href="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;?action=del&id=&lt;?php echo $f['id']; ?&gt;" onclick="return confirm('Delete this file?');"&gt;Delete&lt;/a&gt;]
				&lt;/td&gt;
			
			&lt;/tr&gt;

<?php
}
}

else	
{

?>
<tr><td colspan=“3”>No Files!</td></tr>

<?php
}

?>

	&lt;/table&gt;

<?php
echo “<p><a href=\“logout.php\”>Logout</a></p>”;
include (‘includes/footer.html’);
?>

I noted that line and tried to rectify the problem by looking at the header.html file which is called from the includes file. I understand from what I have read from read is that it could be that I have white spaces in my php.ini file.

Could this be true and if so how do I get rid of white spaces from php.ini. By the way, here is the header.html file which was called by the include function:

<!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><?php echo $page_title;?></title>
<link href=“css/main.css” rel=“stylesheet” type=“text/css” />
</head>

<body>

<table id=“outerTable”>
<tr><td id=“logo”>
<div>
<img src=“” width=“” height=“” alt=“company_logo” />
</div>
</td></tr>
<tr><td id =“mainMenu”>
<div>
<a href=“home.html”>Home</a> |
<a href=“specials.html”>Specials</a> |
<a href=“designers.html”>Participate</a> |
<a href=“aboutAd.html”>About ADesigns</a> |
<a href=“contact.php”>Contact Us</a>
</div>
</td></tr>
<tr><td id=“banner”> <! – page banner image –>
<img src=“” width=“” height=“” alt=“welcome to ADesigns”>
</td></tr>
<tr><td>
<table id=“contentTable”><tr>
<td id=“contentLeft”>
<div id=“leftSideBar”>

				&lt;h3&gt; CATEGORIES&lt;/h3&gt;
				&lt;ul&gt;
					&lt;li&gt;&lt;a href="#"&gt;Jeans&lt;/a&gt;&lt;/li&gt;
					&lt;li&gt;&lt;a href="#"&gt;Pants&lt;/a&gt;&lt;/li&gt;
					&lt;li&gt;&lt;a href="#"&gt;Capris & Shorts&lt;/a&gt;&lt;/li&gt;
					&lt;li&gt;&lt;a href="#"&gt;Skirts&lt;/a&gt;&lt;/li&gt;
					&lt;li&gt;&lt;a href="#"&gt;Dresses&lt;/a&gt;&lt;/li&gt;
					&lt;li&gt;&lt;a href="#"&gt;Sweaters&lt;/a&gt;&lt;/li&gt;
					&lt;li&gt;&lt;a href="#"&gt;Tops&lt;/a&gt;&lt;/li&gt;
					&lt;li&gt;&lt;a href="#"&gt;Ts & Camis&lt;/a&gt;&lt;/li&gt;
				&lt;/ul&gt;
				
				&lt;h3&gt;DESIGNERS&lt;/h3&gt;
				&lt;ul&gt;
					&lt;li&gt;&lt;a href="#"&gt;Fred Perry&lt;/a&gt;&lt;/li&gt;
					&lt;li&gt;&lt;a href="#"&gt;Full CIrcle&lt;/a&gt;&lt;/li&gt;
					&lt;li&gt;&lt;a href="#"&gt;Hannah Smith&lt;/a&gt;&lt;/li&gt;
					&lt;li&gt;&lt;a href="#"&gt;Jacob & Co.&lt;/a&gt;&lt;/li&gt;
					&lt;li&gt;&lt;a href="#"&gt;Lee&lt;/a&gt;&lt;/li&gt;
					&lt;li&gt;&lt;a href="#"&gt;Oasis&lt;/a&gt;&lt;/li&gt;
					&lt;li&gt;&lt;a href="#"&gt;Polo&lt;/a&gt;&lt;/li&gt;
					&lt;li&gt;&lt;a href="#"&gt;Boss&lt;/a&gt;&lt;/li&gt;
				&lt;/ul&gt;
				
			&lt;/div&gt; &lt;! -- leftSideBar --&gt;
		&lt;/td&gt;
		&lt;td id="contentCenter"&gt;
			&lt;div id="mainContent"&gt;

Please help,
Thank you

HI,Friend.your code error at session_start(),the session should put the fist line!
you can try cut session_start() in header.html.just below…


<?php session_start(); ?>
<!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><?php echo $page_title;?></title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
</head>


So are you saying that I should remove session start from loggedin.php or leave it threr and just add a new one in header.html?

Thanks

you must remove session_start from loggin.php and add a new one in header.html.understand?

Understood… Thanks

I removed it and did as you said. I got an error in redirecting the page. could that be because I did not take this out as well ?:
if (!isset($_SESSION[‘user_id’])) {
require_once (‘loginFunctions.inc.php’);
$url = absolute_url();
header("location: ".$url);
exit();
}

@alandy

even after the session was removed, I still got this:

Warning: Cannot modify header information - headers already sent by (output started at /Library/WebServer/Documents/projectAD/includes/header.html:8) in /Library/WebServer/Documents/projectAD/loggedin.php on line 84

Please can you explain why we have to remove the seesion

Thanks

Hi,
OK first up - put the session_start() back in as you need it for other things.

Right then, look at the error message in detail and your answer is there.

PHP says:

  • headers already sent by (output started at /Library/WebServer/Documents/projectAD/includes/header.html:8) in /Library/WebServer/Documents/projectAD/loggedin.php on line 84

which means…

- headers already sent - something has been sent to the browser already
and its on line 8 of header.html
by (output started at /Library/WebServer/Documents/projectAD/includes/header.html:8)

but it should be sent on line 84 of logged in
in /Library/WebServer/Documents/projectAD/loggedin.php on line 84

Have a look at what is on line 8 of header.html
I suspect that the php script processing takes place AFTER header.html is called but it should be BEFORE it.

If you really REALLY need to have that flow then you would need to add


ob_start();

above the line where header.html is called. This surpresses the error but it is better to fix it in the long run!

Also please dont put your email address in posts!

I am sorry for that. will not happen again.
Now, I noticed that the problem is from line 8 of header.html which reads the following:
<title><?php echo $page_title;?></title>

I did this because this header.html file is sorted in an includes file which is called from loggedin.php here:

<?php # loggedin.php #2

// The designer is redirected here from login.php.

session_start(); // Start the session.
// If no session value is present, redirect the user:
if (!isset($_SESSION[‘user_id’])) {
require_once (‘loginFunctions.inc.php’);
$url = absolute_url();
header("location: ".$url);
exit();
}
$page_title = ‘Welcome to ADesigns: Logged In!’;
include (‘includes/header.html’);

Any clues?

I here that it has to do with white spaces in my php.ini file and I do not want to tamper with that at this stage of my work OR I do not know what and how to take them out…

Help please thanks…

It is not to do with your ini file - leave that well alone!
The whitepace reason is if you had a space before the <?php tag at the start of the file.
Your problem is that you have <title> tags being sent to the browser - which is classed as output - before the header("location:… "); is called.

Th bit of code you have there is ok but the error says the problem comes further down the loggedIn.php page on line 84
I would guess thats where you have processed your login routine and redirected the user?

I would suggest using the ob_start() method as a quick fix but would look at how your document is structured. Its better to fix errors than to hide them generally!


session_start(); // Start the session.
// If no session value is present, redirect the use
ob_start();

May I say thanks for the short fix. But you know what?
I took out the php code in the header.html file and tried again but then the error pointed to line 17 which was:

<img src=“” width=“” height=“” alt=“company_logo” />

I know that there are no value for scr height and width but even with values it, it still points to that line 17.

What is happening? please

View the source for the html page that shows the error, to find out the “something has been sent to the browser already”

@pmw57
I have looked at it several times but cannot find it. Maybe you could find what I cannot see:
1
2<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
3 “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
4<html xmlns=“http://www.w3.org/1999/xhtml”>
5
6<head>
7 <meta http-equiv=“Content-Type” content=“text/html; charset=utf-8” />
8 <title><?php echo $page_title;?></title>
9 <link href=“css/main.css” rel=“stylesheet” type=“text/css” />
10</head>
11
12<body>
13
14<table id=“outerTable”>
15 <tr><td id=“logo”>
16 <div>
17 <img src=“” width=“” height=“” alt=“company_logo” />
18 </div>
19 </td></tr>
20 <tr><td id =“mainMenu”>
21 <div>
22 <a href=“home.html”>Home</a> |
23 <a href=“specials.html”>Specials</a> |
24 <a href=“designers.html”>Participate</a> |
25 <a href=“aboutAd.html”>About ADesigns</a> |
26 <a href=“contact.php”>Contact Us</a>
27 </div>
28 </td></tr>
29 <tr><td id=“banner”> <! – page banner image –>
30 <img src=“” width=“” height=“” alt=“welcome to ADesigns”>
31 </td></tr>
32 <tr><td>
33 <table id=“contentTable”><tr>
34 <td id=“contentLeft”>
35 <div id=“leftSideBar”>
36
37 <h3> CATEGORIES</h3>
38 <ul>
39 <li><a href=“#”>Jeans</a></li>
40 <li><a href=“#”>Pants</a></li>
41 <li><a href=“#”>Capris & Shorts</a></li>
42 <li><a href=“#”>Skirts</a></li>
43 <li><a href=“#”>Dresses</a></li>
44 <li><a href=“#”>Sweaters</a></li>
45 <li><a href=“#”>Tops</a></li>
46 <li><a href=“#”>Ts & Camis</a></li>
47 </ul>
48
49 <h3>DESIGNERS</h3>
50 <ul>
51 <li><a href=“#”>Fred Perry</a></li>
52 <li><a href=“#”>Full CIrcle</a></li>
53 <li><a href=“#”>Hannah Smith</a></li>
54 <li><a href=“#”>Jacob & Co.</a></li>
55 <li><a href=“#”>Lee</a></li>
56 <li><a href=“#”>Oasis</a></li>
57 <li><a href=“#”>Polo</a></li>
58 <li><a href=“#”>Boss</a></li>
59 </ul>
60
61 </div> <! – leftSideBar –>
62 </td>
63 <td id=“contentCenter”>
64 <div id=“mainContent”>
65

The rest of the html is stored in another includes file called foot.html because, I make calls to the header and footer.htmls from my php scripts.

Any suggestions?
Thanks

snip

Is that HTML code what appears when you view the source code of the error page that says “Warning: Cannot modify header information - headers already sent …”

The whole reason why the error occurs, is tat you have send output for the page before other parts of the code that update the header.

Yes that is the one… it has been driving me crazy. Although I did get a temp fix from spikez that is the:
ob_start();

but I want to sort it out permanently

Thanks

You can sort it out permanently by ensuring that header changes occur ONLY before page content is echoed to the page.

Please can you elaborate? Basically, when the loggedin.php file processes an upload or delete or download, the http() header functions made for each of those calls must be sent before the : <title><?php echo $page_title;?></title> is sent right?

How can that be achieved please?
Thanks

The headers must be sent before the <html> is sent. They must be sent even before <!DOCTYPE … is sent.

You already have what seems to be an effective manner in which to do that, with the ob_start() function.
After the headers have been updated, you can flush what’s been buffered and end further buffering with ob_end_flush()

If you don’t want to make use of those methods, then you have two other options.

[list=1][]Rearrange your code so that the header statements occur before any output.
[
]Store the planned output to a variable (commonly called $text or $html) and then echo that out after the update to the headers.[/list]

Thanks a lot for the help. I will try those out and get back to you should I encounter any problem.

Thanks

Hi There

I want to implement a site where vistors can have discussion forums as well as members.

Different Question:
In my project I have a database called project with the following tables:

USERS (user_id, username,password, firstname, lastname, email, account_typ,company name)
IMAGES (img_id, user_id, description, mimetype, filename, filedata)
MESSAGES (message_id, subject, body, parent_id,date,user_id, forum_id)
FORUMS (forum_id, name)

account_type has two values = designer or visitor
user_id is a Foreign Key to IMAGES and MESSAGES

Senerio:

Suppose a user comes into the website and registers, his details are stored into the users table, then he can login and upload images. The problem now is that in the database the images table has not been initialized because am am performing all Mysql commands from a PHP script called loggedin.php

  1. How can I make sure that after a registration has been sent, a confirmation is sent to the users email address
  2. How can a user upload images so that each image he uploads is uniquely his because with main uploads are fine but when another user logs into the website, They have access to the same file that was already there

Thank you