setcookie不删除cookie [英] setcookie not removing cookie

查看:165
本文介绍了setcookie不删除cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不知道这里的问题。

I really don't know what the issue is here.

我有一个名为login.php的脚本,它完美地工作。它使用AJAX调用,如果它返回成功,页面刷新并且用户登录。Cookie在该页面上设置

I have a script called "login.php" that works perfectly. It's called with AJAX, and if it returns successfully, the page refreshes and the user is logged in. The cookie is set on that page with

setcookie("main", $row[0], time() + 3600, "/")


$ b b

然后我有一个脚本叫做logout.php。它被称为相同的方式(AJAX然后页刷新)。它只有两行:

Then I have a script called "logout.php". It is called the same way (AJAX and then page refresh). It only has two lines:

<?php
    setcookie("main", "", time() - 3600, "/");
    echo "Done";
?>

从页面调用它不工作,所以我刚刚在浏览器中加载logout.php。输出是完成,但检查我的cookie在Chrome显示我的cookie仍然设置为1(这是$ row [0]),并在login.php设置的时间到期。

Calling it form the page wasn't working, so I just loaded logout.php in the browser. The output was "done" but checking my cookies in Chrome showed me that the cookie was still set to "1" (which was $row[0]) and to expire at the time set in login.php.

login.php和logout.php都在同一个文件夹中,根目录,与其他文件夹是同一个文件夹。

login.php and logout.php are both in the same folder, the root directory, which is the same folder as everything else.

早些时候,这是工作,但我做的唯一的更改是使网站上的标题栏自己的文件(仍在根目录)和采取注销按钮的JavaScript功能,这只是一个AJAX调用和一些jQuery悬浮效果,并使它自己的脚本文件,它在_js文件夹中。但我没有改变logout.php,所以它应该仍然工作,当我直接导航到它,对吧?我的setcookie命令有什么问题,或者什么其他问题可能导致它?

Earlier, this was working, but the only changes I've made are to make the title bar on the website its own file (still in the root directory) and to take the JavaScript functionality for the Logout button, which is just an AJAX call and some jQuery hover effects, and make it its own script file, which is in the _js folder. But I didn't change logout.php at all, so it should still work when I navigate directly to it, right? Is there something wrong with my setcookie command, or what other problem could be causing it?

编辑:我试图设置它在100秒而不是-3600过期,然后尝试更改其名称,以便我可以将其识别为一个完全独立的cookie。他们都没有出现。这个cookie根本没有设置。

I tried setting it to expire in 100 seconds instead of -3600, and then tried changing its name so I could recognize it as a totally separate cookie. Neither of them made it show up. The cookie simply isn't getting set at all.

编辑2:我还原到最后一次提交,一切正常工作。我不知道为什么通过创建一些新文件(logout.php没有改变)使某个脚本无法创建cookie来重组我的网站。

EDIT 2: I reverted to the last commit, and everything is working again. I don't know why reorganizing my site by creating some new files (logout.php isn't changed at all) makes a certain script unable to create cookies.

推荐答案

Cookie必须在任何内容发送到客户端之前设置。确保您在调用setcookie之前没有向客户端发送任何内容,因为它实际上是通过请求返回的 Set-Cookie 头。

Cookies must be set before any content goes to the client. Make sure you haven't sent anything to the client before calling a setcookie as it's actually a Set-Cookie header coming back through the request.

与包含文件中的空格一样少的东西可以毁掉这个。 (你说你在处理其他文件,检查没有包含任何东西,比如你的<?php ...?> 括号之外的调试回声或空格)

Something as little as a space in an include file can ruin this. (You said you worked on other files, check that nothing being included has anything such as a debug echo or whitespace outside of your <?php ... ?> brackets)

作为一般的经验法则,如果我有一个专门的PHP代码的文件,我只包括开始标签(<?php ),并豁免最后一个。 PHP仍然会解析和运行,但这避免了文件末尾的任何可能会出现问题的空格。例如

As a general rule of thumb, if I have a file that is exclusively PHP code I only include the opening tag (<?php) and exempt the last one. PHP will still parse and run, but this avoids any whitespace at the end of the file that may become problematic. e.g.

mycodefile.php

<?php

  define('...', ...);

  function ...(){ }

铬。以chrome( ctrl + shft + i )打开调试器,然后点击网络然后找到您的注销电话并选择它,然后点击标头,验证您的Cookie是否已被清除。 (同样,您也可以点击资源,并在 Cookie 下查看任何集合。

Also, you mentioned using chrome. Open your debugger in chrome (ctrl+shft+i) and click the Network tab. Then find your logout call and select it, then click Headers and verify your cookie is being cleared. (likewise you can click Resources and look under Cookies to view any set.

这篇关于setcookie不删除cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆