/    Sign up×
Community /Pin to ProfileBookmark

How do I do a cookie refresh?

I have a client that wants his site to refresh every 5 minutes. How do i do that? I tried

[code=php]
<?php
$value = ‘something from somewhere’;
setcookie(“TestCookie”, $value, time()+1100); /* expire in 1 hour */
?>

[/code]

That gives me this error
Warning: Cannot modify header information – headers already sent by (output started at /hermes/web05/b1750/pow.fireballdeals/htdocs/Matts Store 2/PS2_Controllers.php:77) in /hermes/web05/b1750/pow.fireballdeals/htdocs/Matts Store 2/PS2_Controllers.php on line 159

And no longer shows anything on the page as far as the database information that it pulls.

Any ideas or suggestions on what I am doing wrong. Ive never messed with cookies so I’m not sure what the coding is or where it needs to go.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscMay 31.2011 — There is something that the browser is reading as output (a blank line or space is counted as output) before you call the setcookie() line.

setcookie() has to occur before any output at all....like this:

[code=php]
<?php

$myCookieVal = "my value";
setcookie("TestCookie",$myCookieVal,time()+1100);

?>
<!doctype html>
<html>
...
[/code]


The following examples illustrate incorrect ways to use setcookie():

[code=php]
<!doctype html>
<html>
....

<?php
$myCookieVal = "my value";
setcookie("TestCookie",$myCookieVal,time()+1100);
?>
[/code]



[code=php]
<?php
$myCookieVal = "my value";
?>
<?php
setcookie("TestCookie",$myCookieVal,time()+1100);
//this doesn't ALWAYS result in an error, but it may because the space between the
//closing ?> and the next <?php may be counted as output in some cases
?>
<!doctype html>
<html>
....
[/code]


In certain rare cases, there's a BOM at the beginning of the file that you can't see and it makes good looking code still spit out the error, if you think this might be happening to you then just start a new blank file and copy and paste your code into it.
×

Success!

Help @shutterbug 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.3,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...