0

On a page with no output, I'm trying to set a cookie with:

//Setcookie
function mySet ($name, $value, $life = 86400)  {
    global $url;
    setcookie($name, $value, time()+$life, "/", $url, null, true);
}

The arguments I pass are 'user' for the name and 'Sink' for the value.

When using var_dump(headers_list());, it returns an empty array (array(0) { })

I tried using header_remove(); on the line before the setcookie just in case, but it still errors out with

Warning: Header may not contain more than a single header, new line detected in /var/www/dota/other/req.php on line 117

I have a file with a bunch of functions (just a ton of functions) that's called on every page - line 117 is the third line in the mySet() function (setcookie()

Additionally, the cookie is not set. However, refreshing the page both removes the error and sets the cookie, even though there's no code on the entire page that checks if the page has been refreshed. I can refresh the page as many times as I want and it works. The only code on the page is:

//Update last login, along with their steam name if they changed it
$query = "UPDATE users SET steamname = ?,loginip = ? WHERE steamid = ? ";
$stmt = mysqli_stmt_init($con);
$stmt->prepare($query);
$stmt->bind_param('sss', $steamprofile['personaname'], $_SERVER["HTTP_CF_CONNECTING_IP"], $steamprofile['steamid']);
$stmt->execute();

//Get the secret key
$query = "SELECT secretkey FROM users WHERE steamid = ? ";
$stmt = mysqli_stmt_init($con);
$stmt->prepare($query);
$stmt->bind_param('s', $steamprofile['steamid']);
$stmt->execute();
$result = mysqli_fetch_assoc($stmt->get_result());

//Set the cookies
header_remove(); 
mySet('userKey', $result['secretkey']);
mySet('user', $steamprofile['personaname']);
var_dump(headers_list());
delCookie('type');
redir('/coach/browse.php');
Jon
  • 2,566
  • 6
  • 32
  • 52

2 Answers2

0

A few things cold be causing this, but most likely it is the var_dump. Anything printing out to the browser will cause a header. If your redir() function also creates a header that would cause the error.

Is there any more to this file?

Also, if the file is pure PHP you should never include the closing PHP tag ?> this could also cause this error if there is an invisible control character in the file.

James Grundner
  • 1,454
  • 1
  • 10
  • 12
  • 2
    OP states that var_dump was added afterward for debugging the error, that it is still there when removed. – mith Apr 16 '15 at 03:11
  • var_dump was added after the issue occurred to debug it. I removed it and it still breaks. redir() echos javascript but it shouldn't break anything as the cookies are set first, right? There is more to this file but it's all just a ton of functions that aren't called. – Jon Apr 16 '15 at 03:11
0

I don't see anything wrong with the code you posted. It could be with other code that has been left out here.

See here for relatively complete answer and things to look for or to try: How to fix "Headers already sent" error in PHP

Especially look at using headers_sent() as a debug tool to find out where your output is starting to get sent over unintentionally.

Community
  • 1
  • 1
mith
  • 226
  • 1
  • 8