简体   繁体   English

用 PHP 读取 cookies

[英]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:这两种情况都可能发生:

  1. User requests your page and (s)he hasn't the cookie.用户请求您的页面并且(s)他没有 cookie。 You render the response via PHP, but you can't see the cookie at server.您通过 PHP 呈现响应,但在服务器上看不到 cookie。 Response gets delivered to the browser and on DOMReady (or events like that) you set the cookie.响应被传递到浏览器并在 DOMReady(或类似的事件)上设置 cookie。 User sends another request (via interaction with your page).用户发送另一个请求(通过与您的页面交互)。 Here you have the cookie at server.在这里,您在服务器上有 cookie。
  2. User already has the cookie (coming back to your site) and sends a request.用户已经拥有 cookie(返回您的站点)并发送请求。 You render the response via PHP, but this time, cookie is available in first shot.您通过 PHP 呈现响应,但这一次,cookie 在第一张照片中可用。 The rest is the same. rest 也是一样。

Two suggestions:两个建议:

  1. To prevent inserting null (or empty) values into your DB, first check to see if cookie exists or not.为防止将 null(或空)值插入数据库,请首先检查 cookie 是否存在。 If not, simply try to set it.如果没有,只需尝试设置它。
  2. For implementing Analytics, predefined patterns exist.为了实施 Analytics,存在预定义的模式。 For example, instead of setting a cookie, you can include your script on every page, and on load of each page, you can get the information you need, and send an asynchronous ajax request to your Analytics Collector PHP file.例如,您可以在每个页面上包含脚本,而不是设置 cookie,在加载每个页面时,您可以获得所需的信息,并向您的 Analytics Collector PHP 文件发送异步 ajax 请求。 No need for cookie:)不需要饼干:)

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

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