PHP_USER1
PHP_USER1

Reputation: 628

download header showing an error on my server

This is my 2nd page , my first page is download button

<?php
session_start();

//user not logged in, no direct access
if (!isset($_SESSION['user'])) header("location:../admin/");

include "../config.php";
if(isset($_POST['downld'])) {
header( "Content-Type: application/vnd.ms-excel" );
header( "Content-disposition: attachment; filename=emc_promo_".date("m-d-Y").".xls" );
$q="select * from emc_leadgen2 order by emc_time_date desc  ";
$r=mysql_query($q);
$count=mysql_num_rows($r);
if($count>0) {?> 
<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
<head>
<meta http-equiv="Content-type" content="text/html;charset=utf-8" />
</head>
<body>
<table cellpadding="3" cellspacing="0"  border="1" >    
<tr><td colspan="8" align="center"><b>Lead Gen Form for EMC on <?=date("m-d-Y")?></b></td></tr>  
<tr>
<td style="padding:5px;"><b>FIRST NAME</b></td>
<td style="padding:5px;"><b>LAST NAME</b></td>
<td style="padding:5px;"><b>NAME OF ORG.</b></td>
<td style="padding:5px;"><b>JOB TITLE</b></td>
<td style="padding:5px;"><b>MAIL</b></td>
<td style="padding:5px;"><b>PHONE</b></td>
<td style="padding:5px;"><b>ADDRESS</b></td>
<td style="padding:5px;"><b>STORAGE</b></td>
<td style="padding:5px;"><b>TIME & DATE</b></td>
</tr>
<?php while($row=mysql_fetch_array($r)) {?>
<tr>
<td style="padding:5px;"><?=$row['emc_fname']?></td>
<td style="padding:5px;"><?=$row['emc_lname']?></td>
<td style="padding:5px;"><?=$row['emc_organization']?></td>
<td style="padding:5px;"><?=$row['emc_job_title']?></td>
<td style="padding:5px;"><?=$row['emc_email']?></td>
<td style="padding:5px;"><?=$row['emc_phone']?></td>  
<td style="padding:5px;"><?=$row['emc_address']?></td>
<td style="padding:5px;"><?=$row['emc_storage']?></td>
<td style="padding:5px;"><?=$row['emc_time_date']?></td>
</tr>
<?php
}
}
}
?>
</table>
</body>
</html>

When i click on the download button , its showing this error

Warning: Cannot modify header information - headers already sent by (output started at /home/content/59/8131259/html/leadyourtran/offer1/config.php:6) in /home/content/59/8131259/html/leadyourtran/offer1/admin/downld.php on line 8

and

Warning: Cannot modify header information - headers already sent by (output started at /home/content/59/8131259/html/leadyourtran/offer1/config.php:6) in /home/content/59/8131259/html/leadyourtran/offer1/admin/downld.php on line 9

my line 8 and 9 are both header one only.

Upvotes: 1

Views: 106

Answers (2)

Optimus Prime
Optimus Prime

Reputation: 6907

Trying adding,

ob_start();

at the top of your php file.

If by default your output_buffering is Off and you have been unfortunate enough to send a single byte of data back to the client then your HTTP headers have already been sent. Which effectively prevents session_start() from passing the cookie header back to the client. By calling ob_start() you enable buffering and therefore delay sending http headers.

Borrowed from another answer,

In the "normal case", I don't think ob_start has to be called before session_start -- nor the other way arround.

Quoting the manual page of session_start, though :

session_start() will register internal output handler for URL rewriting when trans-sid is enabled. If a user uses ob_gzhandler or like with ob_start(), the order of output handler is important for proper output. For example, user must register ob_gzhandler before session start.

But this is some kind of a special case : the thing is, here, that the order of output handlers is important : if you want one handler to modify things the other did, they have to be executed in the "right" order.


Generally, if you don't use that kind of handlers (Apache and mod_deflate do a great job when it comes to compressing output, for instance), the only thing that matters is that headers must not be sent before you call session_start (because, depending on your configuration, session_start sends cookies, which are passed as HTTP headers).

And headers are sent as soon as any piece of data has to be sent -- ie, as soon as there is any output, even one whitespace outside of <?php ?> tags :

Note: If you are using cookie-based sessions, you must call session_start() before anything is outputted to the browser.

ob_start indicates that PHP has to buffer data :

This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.

This way, output is not sent before you actually say, yourself, "send the data". This means headers are not send immediatly -- which means session_start can be called later, even if there should have been output, if ob_start had not been used.


Hope this makes things a bit more clear...

Upvotes: 0

CoursesWeb
CoursesWeb

Reputation: 4237

header(), like session_start() must be called before any output or html code. If you have an output in the file included before header() (or a warning error), results these errors.

Check in your config.php, line 6 (specified in error mesage).

Upvotes: 1

Related Questions