简体   繁体   English

PHP服务器发送一个cookie,浏览器收到它,但未找到cookie

[英]PHP server sends a cookie, browser receives it, but no cookie is found

I have searched and searched, but nothing seems to match my issue. 我进行了搜索,但似乎没有匹配的问题。

In my development environment, I have some PHP scripts to generate HTML to the client. 在我的开发环境中,我有一些PHP脚本可以为客户端生成HTML。 I created my own custom sessions manager, and everything in my code is working (so far) until I try to send a cookie on a new session. 我创建了自己的自定义会话管理器,并且直到尝试在新会话上发送Cookie之前,代码中的所有内容都可以正常工作。 Here is some code to give an idea: 这是一些给出想法的代码:

<?php
    class Session {
        private $data = null;
        private $id = null;
        private $token = null;
        private $access = null;
        private $oldID = null;
        private static $ck_limit = 180;
        private static $ck_domain = "kiosk.local.com";
...
        public function __construct() {
            setcookie("id", $this->id, (time() + self::$ck_limit), '/', self::$ck_domain, true, true);
        }

?>

The browser gets the cookie just fine according to Firebug, but in the cookies list, it's nowhere to be found! 根据Firebug,浏览器可以很好地获取cookie,但是在cookie列表中却找不到它! Page refreshes don't do anything, and no cookies seem to be making it to the client storage. 页面刷新不执行任何操作,并且似乎没有cookie进入客户端存储。 The links are absolute paths, over HTTPS, certs are permanently stored and trusted, and changing between any of these settings yields no cookie. 链接是绝对路径,通过HTTPS,证书被永久存储和信任,并且在这些设置之间进行更改不会产生cookie。

I tested cookie reception with google in the same browser, and they work. 我在同一浏览器中用Google测试了cookie接收,并且它们可以工作。

Just in case someone asks, my machine's hostname is an FQDN of kiosk.local.com and my hosts file has it mapped to its interfaces. 以防万一有人问,我的机器的主机名是kiosk.local.com的FQDN,我的主机文件已将其映射到其接口。 This isn't my first server, but it is the first time I am dealing with cookies in PHP. 这不是我的第一台服务器,但这是我第一次使用PHP处理Cookie。

I ensured that there is NOTHING being sent to the client prior to sending the cookie, and I tried sending it with nothing, an empty page, and the page I am sending it regularly, and tried without output buffering. 我确保在发送Cookie之前没有将任何内容发送给客户端,并且尝试不发送任何内容,发送一个空页面以及我定期发送该页面的页面,并尝试不使用输出缓冲。

Is there any reason this should be happening? 有什么原因会发生这种情况吗? My error.log for apache says nothing about a cookie, and the PHP setcookie() method returns true, and Firebug reports no errors whatsoever on the page load... just no cookie in the cookies list. 我在apache的error.log中没有任何关于cookie的内容,PHP setcookie()方法返回true,并且Firebug在页面加载时未报告任何错误……在cookie列表中没有cookie。 I can see the cookie data in the received header, but that's the only indication that the server is doing its job. 我可以在收到的标头中看到cookie数据,但这只是服务器正在执行其工作的唯一指示。

Browser is Firefox 40.0.3 for Ubuntu 12.04.3 LTS, Extensions are Firebug, FireQuery, Firefox HTTP authentication from sub-resources Hotfix, and Ubuntu Modifications. 浏览器是用于Ubuntu 12.04.3 LTS的Firefox 40.0.3,扩展是Firebug,FireQuery,来自子资源修补程序的Firefox HTTP身份验证和Ubuntu修改。

I believe I found my answer this morning on a hunch. 我相信我今天早上就凭直觉找到了答案。 The server's code is fine. 服务器的代码很好。 What I left out incidentally, because I didn't think it had any impact or bearing on the system, was that the client and the host are the same machine, meaning I am running the browser from a user on the machine that the apache2 server is running from. 顺带一提,因为我认为这对系统没有任何影响或影响,所以客户端和主机是同一台机器,这意味着我正在从用户上运行浏览器,而该机器是apache2服务器正在运行。 My project here is a standalone application that is web-based using PHP and MySQL. 我的项目是一个独立的应用程序,它使用PHP和MySQL基于Web。 I triple-checked that the browser is in fact requesting the proper url (i sent the same url to my external client, so it's not a trivial error on this end). 我仔细检查了浏览器实际上是否在请求正确的URL(我将相同的URL发送给我的外部客户端,因此这不是一个普通的错误)。

I believe the issue is that somehow, if either the browser or the apache2 server determines that a cookie will be sent to a client with the same name as the server, the cookie gets eaten as it is received. 我认为问题在于,如果浏览器或apache2服务器确定以某种方式将cookie发送给与服务器名称相同的客户端,则cookie会在被接收时被吃掉。 Not certain which service is eating the cookie, but clearly Firefox reports receiving the cookie after it is sent, so either Apache is eating it, or Firefox is. 不确定哪个服务在吃cookie,但很明显Firefox报告发送后会收到该cookie,因此Apache正在吃它,或Firefox正在吃。

Regardless of which is the culprit, it is clear to me that cookies cannot be used on a server/client that are the same host/IP, at least when they are Apache2.2.22 and Firefox 40.0.3, even when using an FQDN. 不管是哪一个罪魁祸首,对我来说很清楚,至少在Apache2.2.22和Firefox 40.0.3上,即使在使用FQDN时,也不能在具有相同主机/ IP的服务器/客户端上使用cookie。

So, in short, standalone applications for clients on their own server (conundrum) cannot use cookies from itself. 因此,简而言之,位于自己服务器(难题)上的客户端的独立应用程序无法使用其自身的cookie。

If someone has another reason why this is happening, and a viable workaround that does NOT involve circumventing security measures or otherwise compromising the security of the application, web content, network, or using a proxy, I am open to it. 如果有人有其他原因导致这种情况发生,并且有可行的解决方法而不涉及规避安全措施或以其他方式损害应用程序,Web内容,网络或使用代理的安全性,那么我可以接受。 I say no proxies because this program is self-contained as a kiosk, except for admin access via a wifi AP that the host device creates (no routers). 我之所以没有代理人,是因为该程序作为自助服务亭是独立的,除了通过主机设备创建的wifi AP(无路由器)进行管理员访问外。 There are reasons for this that will not be discussed, just accept the fact that this setup will not change, and that all precautions are taken to ensure proper security for this particular setup. 有一些原因将不予讨论,仅接受此设置不会更改的事实,并已采取所有预防措施来确保此特定设置的适当安全性。

Obviously, if cookies cannot be used on the same host that sends them in any way, I may have no other choice but to introduce another device as the client, which I will have to discuss with the project manager before it is done. 显然,如果无法在以任何方式发送它们的同一主机上使用cookie,那么我可能别无选择,只能引入另一台设备作为客户端,在完成之前必须与项目经理进行讨论。 No, a Virtual Machine is out of the question as the hardware specs are not high enough on the server to run a VM and a host, plus I don't think the architecture/platform combo is supported for being a VM host. 不,虚拟机不成问题,因为服务器上的硬件规格不足以运行VM和主机,而且我不认为架构/平台组合支持作为VM主机。

::EDIT:: ::编辑::

I have figured out the culprit, and fixed the issue. 我找出了罪魁祸首,并解决了这个问题。 Please excuse me for one moment whilst I slap myself for this even happening... 请原谅我一会儿,甚至为这件事打巴掌。

*SLAP* *拍*

Ok, so here is what ended up being the difference between the two browsers: 好的,这就是两种浏览器之间的不同之处:

My Host machine (to the VM that the dev-server is running) browser is set to what I normally use, to remember history, accept cookies under normal circumstances, blah, blah, blah. 我的主机(针对运行开发服务器的VM)浏览器设置为我通常使用的浏览器,以记住历史记录,并在正常情况下接受cookie,等等,等等。 However, due to my wanting the dev environment to emulate the production environment as closely as possible for real feedback, I set the browser to never remember history, and not accept cookies, except from the kiosk's domain. 但是,由于我想让开发环境尽可能接近地模拟生产环境以获取真实反馈,因此我将浏览器设置为永远不记住历史记录,并且不接受来自信息亭域的cookie(除了cookie)。 Apparently, despite the site being in the list of exceptions, and even if accept all cookies is enabled, having private browsing drops all cookies that appear in the headers, and does not report them as rejected for any reason. 显然,尽管站点在例外列表中,并且即使启用了接受所有cookie,但是进行私有浏览都会删除出现在标题中的所有cookie,并且不会由于任何原因将其报告为拒绝。 So, there was no way to see why the cookies weren't sticking around after the browser got them. 因此,没有办法查看为什么在浏览器获取Cookie后它们就不会留下来。 There is a wierd bit though, my external host Firefox Browser still gets cookies even in private browsing... weird that it can do it but the internal client cannot... 虽然有些奇怪,但即使在私人浏览中,我的外部主机Firefox浏览器仍会获取Cookie ...奇怪的是它可以做到,但内部客户端却无法...

So, After changing the internal host's firefox settings to normal browsing, but delete all browsing data on browser close, this issue is solved. 因此,将内部主机的Firefox设置更改为正常浏览,但在浏览器关闭时删除所有浏览数据后,此问题得以解决。 This has the same end result, except data is being written to the disk in cases where we don't want data at all, but everything is deleted at the end of the day, which is no big deal. 这有相同的结果,除了数据写入到在我们不希望在数据所有情况下的磁盘,但一切都在这一天,这也没什么大不了的结尾删除。

So, I will say that Firefox 40.0.3 for Ubuntu x86_64 has a bug that when "always use private browsing mode" is used from the privacy tab in the settings, no cookies will make it past the header reception if they came from its own server face. 因此,我要说的是适用于Ubuntu x86_64的Firefox 40.0.3有一个错误,即在设置的“隐私”选项卡中使用“始终使用私人浏览模式”时,如果cookie来自其自身,则没有cookie会超过标题接收服务器端。 You will have to use a workaround that deletes your browsing data on browser close, and be sure to close the browser regularly. 您将不得不使用一种变通办法,即在关闭浏览器时删除浏览数据,并确保定期关闭浏览器。 Problem is now solved. 现在问题解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM