-1

I'm making a simple download counter but I keep getting a header error as seen on - http://dev.kennyist.com/download.php?file=gvb

<?
error_reporting(-1);

$file= $_GET['file']; 
print $_GET['file'];    
$countf= 'download/' . $file . '.txt';    
print $countf;    
$count = file_exists($countf) ? file_get_contents($countf) : 0;    
file_put_contents($countf, ++$count);    
header("Location: http://dev.kennyist.com/download/[$file].zip");    
die();

?>

Output:

gvbdownload/gvb.txt

Warning: Cannot modify header information - headers already sent by (output started at /home/kennyi81/public_html/dev/download.php:1) in /home/kennyi81/public_html/dev/download.php on line 17

Havelock
  • 6,913
  • 4
  • 34
  • 42
Kennyist
  • 61
  • 1
  • 4
  • 10

3 Answers3

2

You are using print to echo info before you modify header. You can't do this. Just comment out or remove the two print lines and it should work.

Pitchinnate
  • 7,517
  • 1
  • 20
  • 37
  • I don't think that question should be answered. It's been answered only here at SO about a gazillion of times. No effort whatsoever on the OP's side. – Havelock May 01 '13 at 19:13
  • I think the same energy used to post that comment could have been used to link the OP to the question answered somewhere else on SO. Since it was not, an answer doesn't seem out of line. – Mattt May 01 '13 at 19:18
  • I've removed both of them, removed all empty lines but still does not work. – Kennyist May 01 '13 at 19:21
  • @mjayt, well here is [one of the many](http://stackoverflow.com/questions/4241973/warning-cannot-modify-header-information-php) – Havelock May 01 '13 at 19:26
  • Is something including this file or is it called by itself? – Pitchinnate May 01 '13 at 19:36
0

Exactly... you cant have print before header... you cant print anything (not echo, not var_dump)

Check here: http://us.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.

GusPilla
  • 21
  • 4
0

If you can't or you don't want to change your code, you can use ob_start(); at the beginning of your code.

amlette
  • 71
  • 8
mcuadros
  • 4,098
  • 3
  • 37
  • 44