如何用 PHP 制作 cookie
可能的重复:
如何为 uuid 设置 cookie
您好,我想知道如何制作PHP 中的 cookie。我已经研究这个主题几个小时了,但我是 PHP 的新手,不太了解它。我找到了这个脚本,但不知道如何将其植入我的网站,有人可以帮忙吗?
setcookie(name, value, expire, path, domain);
Possible Duplicate:
How to set cookies for uuid
Hello, I would like to know how to make a cookie in PHP. I have researched the topic for a couple of hours already, yet im a newbie to PHP and dont understand it that much. I have found this script, but dont know how to implant it into my website, can anyone help?
setcookie(name, value, expire, path, domain);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你想用你的 cookie 做什么?如果您想跟踪服务器上从请求到请求的信息,您可能需要使用 会话,自动使用cookie。
否则,请继续使用
setcookie
您的应用程序需要其他功能。请注意 PHP 手册中的这段代码:What do you want to do with your cookie? If you want to track information on the server from request-to-request, you probably want to use a session, which automatically uses cookies.
Otherwise, go ahead and use
setcookie
for cookies that your application needs for other functionality. Pay attention to this snippet from the PHP manual:确保将其放在任何内容输出到页面之前(就像处理标题一样),
以便:
make sure you put it before anything is output to the page (like you do with headers)
so:
你已经有了答案。
这将创建一个名为 TestCookie 的 cookie,其值为“myValue”,它将在创建后 1 小时后过期。网站/域是 example.com,您所在的文件夹路径是 /~rasmus/。
有关 setcookie 的更多信息请参见此处:
http://php.net/manual/en/function.setcookie.php
You have the answer.
This creates a cookie called TestCookie with a value of "myValue", it will expire 1 hour from its creation. The website/domain is example.com and the folder path you're in is /~rasmus/.
More information on setcookie here:
http://php.net/manual/en/function.setcookie.php
这是一个基本示例
这将创建一个名为“foobar”的 cookie,其值为“Hello, world!”,并在浏览器关闭时过期。
另外,请确保在任何 HTML 输出之前设置 cookie,否则不会创建它。
要检查其值,请执行下一页的操作。
Here is a basic example
This will create a cookie named "foobar" with the value of "Hello, world!", and will expire when the browser closes.
Also, make sure you set cookies before any HTML output otherwise it won't be created.
To check it's value, do on a following page.