Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.