/    Sign up×
Community /Pin to ProfileBookmark

setcookie failing – works everywhere else but one file

I’ve got setcookie() working in every file except one in my code. I’ve dug and dug and can’t seem to find out why.

The only difference is the one I can’t get it to work with is being called by an include function, rather than being posted to.

I’ve tried to get around the whole thing by making a new file getcookie.php and including it at the very top of the file before anything else is called or any output is generated… still, nothing. I’ve moved it to immediately after I could pull the post data, I’ve checked to see if there’s white space somewhere at the top of the file, anything to cause the setcookie to fail, and nothing.

It’s literally the same line of code in all the files that work, and in the file that doesn’t work.

Any ideas as to why it would work fine in the *same place* in all the other files that are called by post, but not work in the one that’s included?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 04.2016 — Does the file that fail by any chance have a BOM (byte order mark)? If it's saved as UTF-8, some editors will put that at the beginning unless you tell it not to.

Otherwise, see if you get any more info by ensuring you get some error output for now:
[code=php]
<?php
ini_set('display_errors', true);
error_reporting(E_ALL);

setcookie('......');
[/code]
Copy linkTweet thisAlerts:
@NogDogMar 04.2016 — Another thought: watch out for white space at the [i]end[/i] of any file that gets included before the setcookie call. One way to help avoid that is to [i]not[/i] terminate included PHP files with the (optional) closing "?>" tag.
Copy linkTweet thisAlerts:
@barwick11authorMar 04.2016 — Does the file that fail by any chance have a BOM (byte order mark)? If it's saved as UTF-8, some editors will put that at the beginning unless you tell it not to.

Otherwise, see if you get any more info by ensuring you get some error output for now:
[code=php]
<?php
ini_set('display_errors', true);
error_reporting(E_ALL);

setcookie('......');
[/code]
[/QUOTE]


It's possible it's inserting a BOM, but I just tried reopening it in every other encoding I could think of that would show it, and I don't see a BOM.

I put in the error reporting, and sure enough, it can't modify the header, something somewhere is modifying the header:

Warning: Cannot modify header information - headers already sent by (output started at submitscore3.php:172) in submitscore_code3.php on line 44

I went back and re-inserted the getcookie.php at the beginning of the file, and apparently I did some syntax wrong the first time because now it works doing it the hacked way.
×

Success!

Help @barwick11 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...