Jump to content

[SOLVED] Headers Sent


patriotfan

Recommended Posts

I keep getting the headers sent error :/

 

Cannot modify header information - headers already sent

 

   function download($id){
   $p1 = rand('000000','999999');
   $new = 'uploads/downloads/' . $p1 . '' . $id . '';
   $old = 'uploads/downloads/' . $id . '';
   copy($old,$new);
   header('Location: $new');
   unlink($new);
  }

Link to comment
https://forums.phpfreaks.com/topic/173360-solved-headers-sent/
Share on other sites

this means that you have output sent beore that line. Make sure there is no output at all (even HTML output) where that function is called. preferably, put the function call at the very top of the page.

Did, but I still get it

 

<?php 
session_start();  
function download($id){
$p1 = rand('000000','999999');
$new = 'uploads/downloads/' . $p1 . '' . $id . '';
$old = 'uploads/downloads/' . $id . '';
copy($old,$new);
header('Location: $new');
unlink($new);
}
?>

 

Also, read the stickied notes before posting.

 

http://www.phpfreaks.com/forums/index.php/topic,37442.0.html

Sorry, won't do it again

Link to comment
https://forums.phpfreaks.com/topic/173360-solved-headers-sent/#findComment-913852
Share on other sites

Well, read the error message, it states where the output is occurring at that is preventing the headers from working. If you cannot determine what the actual output is, post the whole error message and the file where the output is occurring.

Link to comment
https://forums.phpfreaks.com/topic/173360-solved-headers-sent/#findComment-913853
Share on other sites

dont put the function at the very top, put where the function is called. IE

//the following call
$id = "whatever";
download($id);

 

should be at the top of the page. If in whatever script you have it can't be, then you are going to have to take the header out of the function. Besides, Having header calls in functions like that tends to lead problems like you are having.

 

alternatively, you can put

ob_start();

at the top of the page and

ob_flush();

at the very bottom to get around this. However, make sure you put a die() after the header, as things can go awry if you don't. However, if you find you must take this route, you may want to rethink the design of your function.

 

take a read of ob_start() and flush in the php manual for more info

Link to comment
https://forums.phpfreaks.com/topic/173360-solved-headers-sent/#findComment-913858
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.