-3

I have a simple PHP page that sets cookies for 3 URLS. Problem is, even though I never sent any header information, I still get header errors. How can I write the PHP below without getting these errors?

Complete PHP code:

<?php
    if ($_GET["validate"] == 1)
    {
        setcookie("siteaccess", "AccessGranted", time()+(36*1*1), "/", "b********.com", 0); //Access granted
        setcookie("siteaccess", "AccessGranted", time()+(36*1*1), "/", "b********.net", 0);
        setcookie("siteaccess", "AccessGranted", time()+(36*1*1), "/", "b********.zxq.net", 0);

        $page = urldecode($_GET["preturn"]);
        echo '<meta http-equiv="Refresh" content="0;' . $page . '">';
    }
    else
    {
        echo "Illegal page access";
    }
?>

Error when page loads:

Warning: Cannot modify header information - headers already sent by (output started at /www/zxq.net/b/i/t/bitfracture/htdocs/authenticate.php:2) in /www/zxq.net/b/i/t/bitfracture/htdocs/authenticate.php on line 4

Warning: Cannot modify header information - headers already sent by (output started at /www/zxq.net/b/i/t/bitfracture/htdocs/authenticate.php:2) in /www/zxq.net/b/i/t/bitfracture/htdocs/authenticate.php on line 5

Warning: Cannot modify header information - headers already sent by (output started at /www/zxq.net/b/i/t/bitfracture/htdocs/authenticate.php:2) in /www/zxq.net/b/i/t/bitfracture/htdocs/authenticate.php on line 6

URL at page load:

http://b*******.com/authenticate.php?preturn=%2Fcaptcha%2Findex.php&validate=1

Further explanation: This page sets a cookie for all 3 URLS that my site can be accessed from. This page is loaded after the user completes a simple anti-robot test, made to prevent robot spamming of my download counters. Once the cookies are set, this page redirects you back to the page that sent you here in the first place, and since the cookies should be in place after that, the user will no longer see the authentication captchas.

Please let me know if I need to provide more information. I only need to get these errors to go away so I can set cookies correctly.

Bit Fracture
  • 651
  • 1
  • 9
  • 24

2 Answers2

3

It is a small problem that most people would not think of. However, a single empty line before the PHP code will cause data to be sent. Just eliminate the blank line. I hope this helps somebody!

Bit Fracture
  • 651
  • 1
  • 9
  • 24
  • 2
    It's not a "hypertext data". Your response isn't necessary a HTML, it may be a binary as well – zerkms Apr 26 '13 at 03:32
0

In setcookie()'s documentation:

Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including and tags as well as any whitespace.

The problem is not in the code piece shown in question. There is at least a whitespace before the setcookie() function.

Raptor
  • 53,206
  • 45
  • 230
  • 366