用PHP读取cookie [英] Reading cookies with PHP

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

问题描述

我正在尝试读取我专门用javascript,jQuery Cookie插件设置的cookie,之后我用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.

由于某种原因,cookie是在页面加载时创建的,但在刷新页面之前不会存在。这意味着我将空白字段输入到我的数据库表中。

For some reason the cookie is being created on page load, but doesn't "exist" until the page is refreshed. Which means that I'm pumping blank fields into my database tables.

我能想到的唯一方法是将AJAX输出到创建cookie的脚本。或者ajax到一个脚本,它在json中将数据返回给我。

The only way I can think of doing it is to AJAX out to a script which creates the cookie. Or ajax out to a script which returns the data to me in json.

用例是我正在为内部项目创建一个简单的分析类,而我我想在数据库中写入用户分辨率,颜色深度和所有爵士乐,我正在使用 screen.width 等来获取。

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.

推荐答案

当客户端执行请求时,Cookie数据被发送到服务器(并转发给PHP解释器) 。因此,在从服务器请求页面之后,客户端上的JavaScript设置的cookie将不会被传输,直到下一次请求到同一服务器。

Cookie data are sent to the server (and forwarded to the PHP interpreter) when the client performs the request. 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.

你是什么我们要做的是执行某种请求(可以通过AJAX完成),其中PHP脚本处理传入的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.

< img src =https://i.stack.imgur.com/CXJLI.pngalt =你需要什么>

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

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