输出发送到浏览器后设置 cookie
有没有办法可以在 html 输出后设置 cookie?根据 PHP 手册,setcookie() 应该在输出之前设置。
我的投票系统需要它,其中在成功的 Mysql 查询后将设置一个 cookie。我把它放在一个文件中。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以使用输出缓冲区,因此在脚本顶部添加 ob_start() ,这将创建一个缓冲区,然后您可以设置 cookie,然后结束缓冲区并刷新到浏览器。
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.
从技术上讲没有。如果您想设置 cookie,您需要确保到目前为止没有任何输出发送到浏览器。
没错,不然不行。所以我什至会说必须,而不仅仅是应该。
成功的 mysql 查询本身不会创建任何输出。如果启用错误报告,则只有失败的 mysql 查询才会出现。所以我想知道您是否真的遇到了具体的问题。
mysql 查询本身不应阻止您使用
setcookie
。如果您在使用
setcookie
之前已经完成了 HTML 输出,您需要找到 HTML 输出开始的位置。在该行上方放置ob_start
Docs 函数,该函数将开始输出缓冲。启用输出缓冲后,您的程序仍然可以“输出”HTML,但不会立即发送。然后你应该能够毫无问题地调用
setcookie
:当你的脚本完成时,输出缓冲区将自动发送到浏览器,因此你不需要关心更多,其余的会自动工作。
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.
That's correct, otherwise it won't work. So I would even say must, not only should.
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 theob_start
Docs 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: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.
Cookie 可以在客户端的 JavaScript 中设置 - 有关示例,请参阅此链接:http://www.w3schools.com/js/js_cookies.asp" w3schools.com/js/js_cookies.asp
Cookies can be set in JavaScript on the client side - see this link for examples: http://www.w3schools.com/js/js_cookies.asp
否。Cookie 是在标头中发送的,因此必须在输出正文开始之前设置它们。
但是,您可以使用 PHP 内置缓冲,这样在脚本完全执行完毕之前,它实际上不会生成任何输出。
ob_start
是您想要的函数。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.您可以使用输出缓冲
You can use output buffering
cookie 设置在 http 标头中。如果您想要做的是在投票上设置 cookie,那么您需要执行 header('Location:...') 重定向,或者您可以使用一个小型 iframe,在其中进行相同的 ajax 调用事物。
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.
Cookie 作为响应标头的一部分发送到浏览器。这意味着必须在服务器开始将其响应写入正在处理的请求之前设置它们(以便服务器可以在响应上指定正确的标头)。
我不知道 PHP 中如何处理这个问题的具体细节,但如果我不得不猜测的话,我会说给定 PHP 脚本的输出可能由服务器(通常是 Apache)缓冲,直到脚本完成,然后服务器在 PHP 脚本完成执行后写入响应。如果是这种情况,那么您应该可以随时设置 cookie。如果不是,那么你将无法做到。
我建议简单地测试一下,看看会发生什么。编写一个 PHP 脚本,在执行的最后设置一个 cookie,通过浏览器访问它,然后检查 cookie 是否实际设置。
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.
如果由于某种原因无法缓冲输出,则可以在发送输出后通过在访问设置 cookie 的脚本的当前页面上显示图像来设置 cookie。
mysetcookie.php
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.
mysetcookie.php