PHP 如何操作跨域的COOKIE,不同服务器上
首先是两个不同子域的域名,解析在不同服务器上的,当然目录也不同的。我在进行跨域操作(删除COOKIE)时,失败。无法操作。
我尝试过P3P协议,还是不行,这有办法实现吗?
回复内容:
首先是两个不同子域的域名,解析在不同服务器上的,当然目录也不同的。我在进行跨域操作(删除COOKIE)时,失败。无法操作。
我尝试过P3P协议,还是不行,这有办法实现吗?
cookies 定义在 RFC2109 标准。cookies 的大部分操作,由客户端也就是浏览器实现。
cookies 每一个 cookie 代表着一个 key-value 键值对,以及作用范围,和生命周期。
cookie 字段定义
作用范围(Scope): path
, domain
生命周期(Life cycle):也就是cookie的过期时间,expires
(GMT,UTC) 时间标准
安全作用域(Security Scope):HttpOnly
, Secure
跨域访问涉及 作用范围(Scope), 安全作用域(Security Scope)
如果 你不使用 js 操作 Cookie 的话,可以忽略 安全作用域(Security Scope)。
以上可能有点 抽象,但是你可以打开 chrome 按 F12 -> Resources -> Cookies
可以看到,类似下面这样
以上都是感念跟废话。重要看下面。
服务端只是 cookie 的接收者和解析者。由浏览器也就是客户端决定是否要将哪些 cookies
发送到到服务端。
而客户端是参考标准实现 RFC2109
的话(基本都是这样)。是根据 cookie 的 path, 和 domain 来发送。
我们假设,我们现在有两个 domain.
- oauth2.php123.com
- www.php123.com
如果想让 客户端将 www.php123.com
下面的 cookie 也发送到 oauth2.php123.com
下面。那么你需要,在 setcookie() 的时候,将 domain
设置成为 php123.com
。
<code>php 库函数 setcookie 的定义 bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] ) </code>
来个实在点的例子吧。
<code>php</code><code>// oauth.php123.com/cookie.php <?php setcookie("foo1", "bar1", time() + 3600, "/", "php123.com") setcookie("foo2", "bar1", time() + 3600) //domain 默认使用,当前domain。 setcookie("foo3", "bar3", time() + 3600, "/", "www.php123.com") </code></code>
<code>php</code><code>// www.php123.com/cookie.php <?php print_r($_COOKIE) // output array( 'foo1' => 'bar1', 'foo3'=> 'bar3' ) // 没有拿到 ['foo2' => 'bar2'],那是因为客户端只需将 domain 为`php123.com`,`www.php123.com`的 cookie 发送到 www.php123.com 这个域上。所以 cookie.php 只拿到了该拿到的两个 cookie。 </code>
以上代码可以很好的解释 cookie 的工作方式。理解好 cookie 的工作及机制可以实现很多高级功能。比如分布式的 session 共享。
反之亦然,自己去领悟吧。
1、楼上给出了子域名下的解决方案。很简单,在每次调用时,指定cookie的域为子域名。完全可以采用此方案。
http://stackoverflow.com/questions/22029530/sessions-cookies-shared-on-subdomains/22030121#22030121
2、P3P协议。是解决cookie跨域时的方案。a站生成cookie,b站删除a站cookie。
b站加js跨域:
<code><?php echo '<script src="http//www.a.com/delete-site-a-cookie.php?cookie_name=test">'; </code>
a站接收参数,执行删除
<code><?php //filter_cookie_name(); setcookie($cookie_name, '', time()-3600, '/'); </code></code>
但是,ie下会失效。。。原因么事浏览器安全策略的问题了,所以就得用到p3p.在删除前,加上协议:
<code><?php header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"'); //filter_cookie_name(); setcookie(); </code></code>
现在,你再看看。。
跨域跟PHP 没关系,cookie是通过请求头传递的,PHP 收到了请求必然能得到cookie
如果是www.a.com,和www.b.com, 是没有办法实现跨域操作的, 可以用变通的方法,比如jsonp等来解决。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.
