Home php教程 php手册 实现跨域名Cookie转http//www.phprecord.com

实现跨域名Cookie转http//www.phprecord.com

Jun 13, 2016 am 10:27 AM
cookie http author domain name accomplish change

作者:Christopher Kings-Lynne 翻译:limodou   Cookie真是一个伟大的发明,它允许web开发者保留他们的用户的登录状态。然而,当你的站点或网络 有一个以上的域名时就会出现问题了。   在Cookie规范上说,一个cookie只能用于一个域名,不能够发给其它的域名。因此,如果在浏览器中对 一个域名设置了一个cookie,这个cookie对于其它的域名将无效。如果你想让你的用户从你的站点中的其中 一个进行登录,同时也可以在其它域名上进行登录,这可真是一个大难题。   我的解决方案将使用下面的一般框架: 一个预置的脚本将用来接受通过GET或COOKIE方式传递过来的sessionid号。它将比COOKIE优先选择GET 变量。所以,无论何时需要引用交叉的域名时,我们把sessionid做为一个URL参数进行发送。 修改Apache配置,用来实现重写所有的交叉域名的cookie。这样做的原因一会儿就会清楚了。 在任何时候出现一个交叉域名引用时使用变量。 第一步:创建预置脚本   将下面的代码加到预置脚本中(或出现在所有脚本之前的函数中)。   一旦这个代码运行之后,一个全局的sessionid变量将可以用于脚本。它将保存着用户的cookie中的 sessionid值,或者是通过GET请求发来的sessionid值。 第二步:为所有的交叉域名引用使用变量   创建一个全局的配置文件,用于存放可以进行切换的域名的基本引用形式。例如,如果我们拥有 domain1.com和domain2.com,则如下设置:   现在,如果在代码中如下做:   你将产生如下的输出: 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 ^/-(.*)-(.*?.*)$ $2&sessionid=$1 [L,R,QSA] RewriteRule ^/-(.*)-(.*)$ $2?sessionid=$1 [L,R,QSA] DocumentRoot /usr/local/www/domain2 ServerName www.domain2.com RewriteEngine on RewriteRule ^/-(.*)-(.*?.*)$ $2&sessionid=$1 [L,R,QSA] RewriteRule ^/-(.*)-(.*)$ $2?sessionid=$1 [L,R,QSA]   这些重写的规则实现了上面两个URL重写的要求。 结论   通过使用变量结合与apache的重写功能,交叉域名cookie可以以一种简单的方式实现。想要维护这样的 系统,无论什么时候链接交叉域名,在使用域名变量之外,什么也不用作了!在域名内部的链接不需要进行 修改,因为cookie会工作正常。   如果你有兴趣看一下在生产网络中实际运作中的系统,请参观http://www.familyhealth.com.au/。在 一些交叉域名链接上移动你的鼠标,并且看一下当你点击后它们是如何被重写的。   也许,使用这个技术唯一的问题就是无法删除在用户浏览器中的全部域名下的cookie。 转自:PHPBuilder.com

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

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

How to implement the WeChat clone function on Huawei mobile phones How to implement the WeChat clone function on Huawei mobile phones Mar 24, 2024 pm 06:03 PM

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.

PHP Programming Guide: Methods to Implement Fibonacci Sequence PHP Programming Guide: Methods to Implement Fibonacci Sequence Mar 20, 2024 pm 04:54 PM

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

PHP Game Requirements Implementation Guide PHP Game Requirements Implementation Guide Mar 11, 2024 am 08:45 AM

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

Master how Golang enables game development possibilities Master how Golang enables game development possibilities Mar 16, 2024 pm 12:57 PM

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.

How to implement HTTP streaming using C++? How to implement HTTP streaming using C++? May 31, 2024 am 11:06 AM

How to implement HTTP streaming in C++? Create an SSL stream socket using Boost.Asio and the asiohttps client library. Connect to the server and send an HTTP request. Receive HTTP response headers and print them. Receives the HTTP response body and prints it.

PHP domain name redirection example demonstration and effect display PHP domain name redirection example demonstration and effect display Mar 28, 2024 am 08:21 AM

PHP domain name redirection is one of the commonly used technologies in website development. Through domain name redirection, users can automatically jump to another URL when visiting one URL, thereby achieving website traffic guidance, brand promotion and other purposes. The following will use a specific example to demonstrate the implementation method of PHP domain name redirection and show the effect. Create a simple PHP file named redirect.php with the following code:

How to register a corporate email domain name How to register a corporate email domain name May 07, 2024 pm 01:48 PM

1. Choose an available domain name: The corporate email domain name should be related to the corporate brand or business. 2. Check whether the domain name has been registered: After selecting the corporate email domain name, you need to check whether the domain name has been registered by other companies. 3. Choose an email service provider: Enterprises can search for different email service providers through search engines and choose a provider based on their needs. 4. Create an account: The process of registering for an email service may be slightly different, but you usually need to fill in the basic information of the company and administrator and create an administrator account.

See all articles