如何用 PHP 制作 cookie

发布于 2024-11-02 18:33:25 字数 345 浏览 1 评论 0原文

可能的重复:
如何为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一紙繁鸢 2024-11-09 18:33:25

你想用你的 cookie 做什么?如果您想跟踪服务器上从请求到请求的信息,您可能需要使用 会话,自动使用cookie。

否则,请继续使用 setcookie您的应用程序需要其他功能。请注意 PHP 手册中的这段代码:

setcookie() 定义一个 cookie
与 HTTP 的其余部分一起发送
标头。与其他标头一样,cookie
必须在任何输出之前发送
你的脚本(这是一个协议
限制)。这需要你
在之前调用此函数
任何输出,包括和
标签以及任何空格。


<?php setcookie("cookiename", uniqid()); ?> 

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:

setcookie() defines a cookie to be
sent along with the rest of the HTTP
headers. Like other headers, cookies
must be sent before any output from
your script (this is a protocol
restriction). This requires that you
place calls to this function prior to
any output, including and
tags as well as any whitespace.


<?php setcookie("cookiename", uniqid()); ?> 
薄荷梦 2024-11-09 18:33:25

确保将其放在任何内容输出到页面之前(就像处理标题一样),

以便:

<?php

//fill in with the info you want
$name = 'theCookie';
$value = 'tasty';
$expire = null;
$path = '/';
$domain = null;

setcookie($name, $value, $expire, $path, $domain);

...rest of code

?>

make sure you put it before anything is output to the page (like you do with headers)

so:

<?php

//fill in with the info you want
$name = 'theCookie';
$value = 'tasty';
$expire = null;
$path = '/';
$domain = null;

setcookie($name, $value, $expire, $path, $domain);

...rest of code

?>
等数载,海棠开 2024-11-09 18:33:25

你已经有了答案。

<?php
setcookie("TestCookie", "myValue", time()+3600, "/~rasmus/", ".example.com", 1);
?>

这将创建一个名为 TestCookie 的 cookie,其值为“myValue”,它将在创建后 1 小时后过期。网站/域是 example.com,您所在的文件夹路径是 /~rasmus/。

有关 setcookie 的更多信息请参见此处:
http://php.net/manual/en/function.setcookie.php

You have the answer.

<?php
setcookie("TestCookie", "myValue", time()+3600, "/~rasmus/", ".example.com", 1);
?>

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

坏尐絯℡ 2024-11-09 18:33:25

这是一个基本示例

<? setcookie("foobar", "Hello, world!", -1) ?>

这将创建一个名为“foobar”的 cookie,其值为“Hello, world!”,并在浏览器关闭时过期。

另外,请确保在任何 HTML 输出之前设置 cookie,否则不会创建它。

要检查其值,请执行下一页的操作。

<? echo $_COOKIE['foobar']; ?>

Here is a basic example

<? setcookie("foobar", "Hello, world!", -1) ?>

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.

<? echo $_COOKIE['foobar']; ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文