user874737
user874737

Reputation: 543

Set a cookie after output has been send to browser

Is there a way that I can set a cookie after an html output? According to PHP manual setcookie() should be set before the output.

I need it for my voting system wherein a cookie will be set after a successful Mysql query. I made it in one file.

Upvotes: 7

Views: 12911

Answers (8)

Staci S
Staci S

Reputation: 31

If for some reason you can't buffer the output, you can set a cookie after sending output by displaying an image on the current page that accesses a script that sets a cookie.

echo ('<img src="http://example.com/mysetcookie.php?uid='.$userId.'">');

mysetcookie.php

<?php
setcookie('cookie-name',$_REQUEST['uid']);
//output an image - this is a one-pixel clear image
header('Content-Type: image/gif');
echo base64_decode('R0lGODlhAQABAJAAAP8AAAAAACH5BAUQAAAALAAAAAABAAEAAAICBAEAOw==');     
exit;
?>

Upvotes: 1

hakre
hakre

Reputation: 198237

Is there a way that I can set a cookie after an html output?

Technically no. If you would like to set a cookie you need to ensure that no output has been send to the browser so far.

According to PHP manual setcookie() should be set BEFORE the output.

That's correct, otherwise it won't work. So I would even say must, not only should.

I need it for my voting system wherein a cookie will be set after a successful mysql query.

A successful mysql query on it's own will not create any output. Only a failed mysql query would if error reporting is enabled. So I wonder if you actually ran into a concrete problem or not.

The mysql query itself should not prevent you from using setcookie.

In case you have done already HTML output prior the use of setcookie you need to find the place where your HTML output started. Above that line place the ob_startDocs function which will start output buffering.

With output buffering enabled, your program can still "output" HTML but it will not be send immediately. Then you should be able to call setcookie with no problems:

<?php ob_start(); ?>
<html><body>
<?php 
  $result = mysql_run_query($query);
  echo '<h1>Your Voting Results</h1>';
  output_voting_result($result);
  set_cookie('vote', $result['id']);
?>
</body></html>

The output buffer will be automatically send to the browser when your script finishes, so there is not much more you need to care about, the rest works automatically.

Upvotes: 9

Barry Kaye
Barry Kaye

Reputation: 7759

Cookies can be set in JavaScript on the client side - see this link for examples: http://www.w3schools.com/js/js_cookies.asp

Upvotes: 5

tttony
tttony

Reputation: 5092

You can use output buffering

ob_start();

// send output
// set cookie

ob_end_flush();

Upvotes: 2

aroth
aroth

Reputation: 54854

Cookies are sent to the browser as part of the response header. This means that they must be set before the server starts writing its response to the request that is being processed (so that the server can specify the correct headers on the response).

I don't know the specifics about how this is handled in PHP, but if I had to guess I would say that the output of a given PHP script is probably buffered by the server (typically Apache) until the script completes, and then the server writes the response after the PHP script has completed execution. If this is the case, then you should be able to set cookies whenever you want. If it isn't, then you won't be able to.

I'd suggest simply testing it to see what happens. Write a PHP script that sets a cookie at the very end of its execution, access it via a browser, and then check to see if the cookie is actually set.

Upvotes: 1

gview
gview

Reputation: 15421

The cookie gets set in the http header. If what you want to do is set the cookie on a vote, then you either need to do a header('Location:...') redirect, or you could have a small iframe where you make an ajax call that does the same thing.

Upvotes: 1

bigkm
bigkm

Reputation: 2277

you can use the output buffers so at the top of your script you add ob_start() and this will create a buffer and you can then set the cookie and then end the buffer and flush out to the browser.

ob_start();
/* set cookie */
ob_end_flush();

Upvotes: 12

Cameron Skinner
Cameron Skinner

Reputation: 54536

No. Cookies are sent in the header so they must be set before the output body begins.

You can, however, use PHPs built-in buffering so it won't actually generate any output until the script has completely finished executing.

ob_start is the function you want.

Upvotes: 3

Related Questions