[英]Reading cookies with PHP
I am trying to read a cookie which I've set with javascript, jQuery Cookie Plugin specifically, and then after that I'm reading it with PHP to write it into a database.我正在尝试读取我使用 javascript、jQuery Cookie 插件设置的 cookie,然后我使用 PHP 读取它以将其写入数据库。
For some reason the cookie is being created on page load, but doesn't "exist" until the page is refreshed.出于某种原因,cookie 是在页面加载时创建的,但在页面刷新之前并不“存在”。 Which means that I'm pumping blank fields into my database tables.
这意味着我将空白字段注入我的数据库表中。
The only way I can think of doing it is to AJAX out to a script which creates the cookie.我能想到的唯一方法是将 AJAX 输出到创建 cookie 的脚本中。 Or ajax out to a script which returns the data to me in json.
或者 ajax 输出到一个脚本,该脚本将 json 中的数据返回给我。
The use case is that I'm creating a simple analytics class for an internal project, and I'd like to write into the database the users resolution, colour depth and all that jazz, which I'm using screen.width
etc to get.用例是我正在为一个内部项目创建一个简单的分析 class,我想将用户分辨率、颜色深度和所有爵士乐写入数据库,我正在使用
screen.width
等来获取.
Cookie data are sent to the server (and forwarded to the PHP interpreter) when the client performs the request .当客户端执行请求时, Cookie 数据被发送到服务器(并转发到 PHP 解释器)。 Therefore, a cookie set by JavaScript on the client after the page has been requested from the server will not be transmitted until the next request to same server.
因此,在向服务器请求页面后,客户端上由 JavaScript 设置的 cookie 将在下一次向同一服务器请求之前不会传输。
What you'll have to do is to perform some kind of request (could be done via AJAX) where a PHP script handles the incoming cookie information and stores it in the DB.您需要做的是执行某种请求(可以通过 AJAX 完成),其中 PHP 脚本处理传入的 cookie 信息并将其存储在数据库中。
@jensgram is right. @jensgram 是对的。 These two scenarios can happen:
这两种情况都可能发生:
Two suggestions:两个建议:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.