实现跨域名Cookie_PHP
Cookie
Cookie真是一个伟大的发明,它允许web开发者保留他们的用户的登录状态。然而,当你的站点或网络有一个以上的域名时就会出现问题了。
在Cookie规范上说,一个cookie只能用于一个域名,不能够发给其它的域名。因此,如果在浏览器中对一个域名设置了一个cookie,这个cookie对于其它的域名将无效。如果你想让你的用户从你的站点中的其中一个进行登录,同时也可以在其它域名上进行登录,这可真是一个大难题。
我的解决方案将使用下面的一般框架:
一个预置的脚本将用来接受通过GET或COOKIE方式传递过来的sessionid号。它将比COOKIE优先选择GET
变量。所以,无论何时需要引用交叉的域名时,我们把sessionid做为一个URL参数进行发送。修改Apache配置,用来实现重写所有的交叉域名的cookie。这样做的原因一会儿就会清楚了。在任何时候出现一个交叉域名引用时使用变量。
第一步:创建预置脚本
将下面的代码加到预置脚本中(或出现在所有脚本之前的函数中)。
/* 支持交叉域名cookie... */
// 如果GET变量已经设置了,并且它与cookie变量不同
//则使用get变量(更新cookie)
global $HTTP_COOKIE_VARS, $HTTP_GET_VARS;
if (isset($sessionid) && isset($HTTP_GET_VARS['sessionid']) && ($HTTP_COOKIE_VARS['sessionid'] != $HTTP_GET_VARS['sessionid'])) {
SetCookie('sessionid', $HTTP_GET_VARS['sessionid'], 0, '/', '');
$HTTP_COOKIE_VARS['sessionid'] = $HTTP_GET_VARS['sessionid'];
$sessionid = $HTTP_GET_VARS['sessionid'];
}
?>
一旦这个代码运行之后,一个全局的'sessionid'变量将可以用于脚本。它将保存着用户的cookie中的
sessionid值,或者是通过GET请求发来的sessionid值。
第二步:为所有的交叉域名引用使用变量
创建一个全局的配置文件,用于存放可以进行切换的域名的基本引用形式。例如,如果我们拥有
domain1.com和domain2.com,则如下设置:
$domains['domain1'] = "http://www.domain1.com/-$sessionid-";
$domains['domain2'] = "http://www.domain2.com/-$sessionid-";
?>
现在,如果在代码中如下做:
echo "Click here to contact us.";
?>
你将产生如下的输出:
Click here
to contact us.
在这里sessionid已经被插入到URL中去了。
在这个地方,你可能会想"这样可能会在web服务器上打开名为横线,sessionid,横线的子目录?!?!?"。
然而,下面的步骤将提供一个必需的戏法,以便让它能够使用!
第三步:配置Apache
现在,剩下的步骤就是配置apache来重写这个URL:
http://www.domain2.com/-66543afe6543asdf6asd-/contact/
变成这样:
http://www.domain2.com/contact/?sessionid=66543afe6543asdf6asd
并且这种url:
http://www.domain2.com/-66543afe6543asdf6asd-/contact/?email=yes
变成这样:
http://www.domain2.com/contact/?email=yes&sessionid=66543afe6543asdf6asd
为了实现它,简单地配置两个虚拟服务器,作为domain1和domain2,如下操作:
DocumentRoot /usr/local/www/domain1
ServerName www.domain1.com
RewriteEngine on
RewriteRule ^/-(.*)-(.*?.*)$ &sessionid= [L,R,QSA]
RewriteRule ^/-(.*)-(.*)$ ?sessionid= [L,R,QSA]
DocumentRoot /usr/local/www/domain2
ServerName www.domain2.com
RewriteEngine on
RewriteRule ^/-(.*)-(.*?.*)$ &sessionid= [L,R,QSA]
RewriteRule ^/-(.*)-(.*)$ ?sessionid= [L,R,QSA]
这些重写的规则实现了上面两个URL重写的要求。
结论
通过使用变量结合与apache的重写功能,交叉域名cookie可以以一种简单的方式实现。想要维护这样的
系统,无论什么时候链接交叉域名,在使用域名变量之外,什么也不用作了!在域名内部的链接不需要进行
修改,因为cookie会工作正常。
如果你有兴趣看一下在生产网络中实际运作中的系统,请参观http://www.familyhealth.com.au/。在
一些交叉域名链接上移动你的鼠标,并且看一下当你点击后它们是如何被重写的。
也许,使用这个技术唯一的问题就是无法删除在用户浏览器中的全部域名下的cookie。

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











How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

jQuery is a JavaScript library widely used in web development. It provides many simple and convenient methods to operate web page elements and handle events. In actual development, we often encounter situations where we need to determine whether a variable is empty. This article will introduce several common methods of using jQuery to determine whether a variable is empty, and attach specific code examples. Method 1: Use the if statement to determine varstr="";if(str){co
