如何使用 MAMP + 在本地主机上设置 cookie MacOSx + PHP?

发布于 2024-07-15 00:42:07 字数 564 浏览 3 评论 0原文

我正在 Mac 笔记本上进行开发,我使用 MAMP。 我正在尝试使用 PHP 设置 cookie,但不能。 我已经放弃了域,我尝试使用“\”作为域。 没有运气。

setcookie("username", "George", false, "/", false);
setcookie("name","Joe");

我一定错过了一些明显的东西。 我需要一个快速而简单的解决方案。 有吗?

我没有做任何花哨的事情,只是加载(通过 MAMP)页面, http://localhost:8888/MAMP/lynn/setcookie.php

该脚本有setcookie 代码位于顶部,甚至在编写 HTML 标记之前。 (尽管我也在身体中尝试过)。 我在各种浏览器中加载该页面,然后打开 cookie 列表。 我知道浏览器接受 cookie,因为我在列表中看到了当前的浏览器。 只是不是我的新的。

I'm developing on my Mac notebook, I use MAMP. I'm trying to set a cookie with PHP, and I can't. I've left off the domain, I've tried using "\" for the domain. No luck.

setcookie("username", "George", false, "/", false);
setcookie("name","Joe");

I must be missing something obvious. I need a quick and simple solution to this. Is there one?

I'm not doing anything fancy, simply loading (via MAMP) the page,
http://localhost:8888/MAMP/lynn/setcookie.php

That script has the setcookie code at the top, prior to even writing the HTML tags. (although I tried it in the BODY as well). I load the page in various browsers, then open the cookie listing. I know the browsers accept cookies, because I see current ones in the list. Just not my new one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

家住魔仙堡 2024-07-22 00:42:08

取出“localhost”并只使用一个空白字符串对我有用。

Taking out "localhost" and just having a blank string worked for me.

ゞ花落谁相伴 2024-07-22 00:42:08
<?php
ob_start();
if (isset($_COOKIE['test'])) {
    echo 'cookie is fine<br>';
    print_r($_COOKIE);
} else {
    setcookie('test', 'cookie test content', time()+3600);  /* expire in 1 hour */
    echo 'Trying to set cookie. Reload page plz';    
}

尝试这个。

<?php
ob_start();
if (isset($_COOKIE['test'])) {
    echo 'cookie is fine<br>';
    print_r($_COOKIE);
} else {
    setcookie('test', 'cookie test content', time()+3600);  /* expire in 1 hour */
    echo 'Trying to set cookie. Reload page plz';    
}

Try this.

天邊彩虹 2024-07-22 00:42:07

来自文档:

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

是这样吗?

编辑:

您能看到服务器发送的 cookie,例如通过使用 Firefox 扩展Tamper Data 或 telnet 发送吗? 您能看到浏览器在下一个请求时将其发送回来吗? setcookie()的返回值是多少? 它不是在所有浏览器中都有效,还是仅在某些浏览器中有效?

From the docs:

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.

Is that it?

edit:

Can you see the cookie being sent by the server, e.g. by using the Firefox extension Tamper Data, or telnet? Can you see it being sent back by the browser on the next request? What's the return value of setcookie()? Is it not working in all browsers, or just in some?

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