亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home php教程 php手冊 實(shí)現(xiàn)跨域名Cookie

實(shí)現(xiàn)跨域名Cookie

Jun 21, 2016 am 09:14 AM
cookie http quot

cookie

Cookie真是一個偉大的發(fā)明,它允許web開發(fā)者保留他們的用戶的登錄狀態(tài)。然而,當(dāng)你的站點(diǎn)或網(wǎng)絡(luò)
有一個以上的域名時就會出現(xiàn)問題了。


  在Cookie規(guī)范上說,一個cookie只能用于一個域名,不能夠發(fā)給其它的域名。因此,如果在瀏覽器中對
一個域名設(shè)置了一個cookie,這個cookie對于其它的域名將無效。如果你想讓你的用戶從你的站點(diǎn)中的其中
一個進(jìn)行登錄,同時也可以在其它域名上進(jìn)行登錄,這可真是一個大難題。


  我的解決方案將使用下面的一般框架:

一個預(yù)置的腳本將用來接受通過GET或COOKIE方式傳遞過來的sessionid號。它將比COOKIE優(yōu)先選擇GET
變量。所以,無論何時需要引用交叉的域名時,我們把sessionid做為一個URL參數(shù)進(jìn)行發(fā)送。
修改Apache配置,用來實(shí)現(xiàn)重寫所有的交叉域名的cookie。這樣做的原因一會兒就會清楚了。
在任何時候出現(xiàn)一個交叉域名引用時使用變量。
第一步:創(chuàng)建預(yù)置腳本
  將下面的代碼加到預(yù)置腳本中(或出現(xiàn)在所有腳本之前的函數(shù)中)。


/* 支持交叉域名cookie... */

// 如果GET變量已經(jīng)設(shè)置了,并且它與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'];
}

?>

  一旦這個代碼運(yùn)行之后,一個全局的'sessionid'變量將可以用于腳本。它將保存著用戶的cookie中的
sessionid值,或者是通過GET請求發(fā)來的sessionid值。


第二步:為所有的交叉域名引用使用變量
  創(chuàng)建一個全局的配置文件,用于存放可以進(jìn)行切換的域名的基本引用形式。例如,如果我們擁有
domain1.com和domain2.com,則如下設(shè)置:


$domains['domain1'] = "http://www.domain1.com/-$sessionid-";
$domains['domain2'] = "http://www.domain2.com/-$sessionid-";

?>

  現(xiàn)在,如果在代碼中如下做:


echo "Click here to contact us.";

?>
  你將產(chǎn)生如下的輸出:

Click here
to contact us.

  在這里sessionid已經(jīng)被插入到URL中去了。

  在這個地方,你可能會想"這樣可能會在web服務(wù)器上打開名為橫線,sessionid,橫線的子目錄?!?!?"。
然而,下面的步驟將提供一個必需的戲法,以便讓它能夠使用!


第三步:配置Apache
  現(xiàn)在,剩下的步驟就是配置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
  為了實(shí)現(xiàn)它,簡單地配置兩個虛擬服務(wù)器,作為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]


  這些重寫的規(guī)則實(shí)現(xiàn)了上面兩個URL重寫的要求。

結(jié)論
  通過使用變量結(jié)合與apache的重寫功能,交叉域名cookie可以以一種簡單的方式實(shí)現(xiàn)。想要維護(hù)這樣的
系統(tǒng),無論什么時候鏈接交叉域名,在使用域名變量之外,什么也不用作了!在域名內(nèi)部的鏈接不需要進(jìn)行
修改,因為cookie會工作正常。

  如果你有興趣看一下在生產(chǎn)網(wǎng)絡(luò)中實(shí)際運(yùn)作中的系統(tǒng),請參觀http://www.familyhealth.com.au/。在
一些交叉域名鏈接上移動你的鼠標(biāo),并且看一下當(dāng)你點(diǎn)擊后它們是如何被重寫的。

  也許,使用這個技術(shù)唯一的問題就是無法刪除在用戶瀏覽器中的全部域名下的cookie。





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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

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)

Hot Topics

PHP Tutorial
1488
72
Where are cookies stored? Where are cookies stored? Dec 20, 2023 pm 03:07 PM

Cookies are usually stored in the cookie folder of the browser. Cookie files in the browser are usually stored in binary or SQLite format. If you open the cookie file directly, you may see some garbled or unreadable content, so it is best to use Use the cookie management interface provided by your browser to view and manage cookies.

Where are the cookies on your computer? Where are the cookies on your computer? Dec 22, 2023 pm 03:46 PM

Cookies on your computer are stored in specific locations on your browser, depending on the browser and operating system used: 1. Google Chrome, stored in C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data\Default \Cookies etc.

Understand common application scenarios of web page redirection and understand the HTTP 301 status code Understand common application scenarios of web page redirection and understand the HTTP 301 status code Feb 18, 2024 pm 08:41 PM

Understand the meaning of HTTP 301 status code: common application scenarios of web page redirection. With the rapid development of the Internet, people's requirements for web page interaction are becoming higher and higher. In the field of web design, web page redirection is a common and important technology, implemented through the HTTP 301 status code. This article will explore the meaning of HTTP 301 status code and common application scenarios in web page redirection. HTTP301 status code refers to permanent redirect (PermanentRedirect). When the server receives the client's

HTTP 200 OK: Understand the meaning and purpose of a successful response HTTP 200 OK: Understand the meaning and purpose of a successful response Dec 26, 2023 am 10:25 AM

HTTP Status Code 200: Explore the Meaning and Purpose of Successful Responses HTTP status codes are numeric codes used to indicate the status of a server's response. Among them, status code 200 indicates that the request has been successfully processed by the server. This article will explore the specific meaning and use of HTTP status code 200. First, let us understand the classification of HTTP status codes. Status codes are divided into five categories, namely 1xx, 2xx, 3xx, 4xx and 5xx. Among them, 2xx indicates a successful response. And 200 is the most common status code in 2xx

Where are the mobile cookies? Where are the mobile cookies? Dec 22, 2023 pm 03:40 PM

Cookies on the mobile phone are stored in the browser application of the mobile device: 1. On iOS devices, Cookies are stored in Settings -> Safari -> Advanced -> Website Data of the Safari browser; 2. On Android devices, Cookies Stored in Settings -> Site settings -> Cookies of Chrome browser, etc.

An in-depth study of the causes and solutions of 404 errors An in-depth study of the causes and solutions of 404 errors Feb 25, 2024 pm 12:21 PM

Explore the causes and solutions of HTTP status code 404 Introduction: In the process of browsing the web, we often encounter HTTP status code 404. This status code indicates that the server was unable to find the requested resource. In this article, we will explore the causes of HTTP status code 404 and share some solutions. 1. Reasons for HTTP status code 404: 1.1 Resource does not exist: The most common reason is that the requested resource does not exist on the server. This may be caused by the file being accidentally deleted, incorrectly named, incorrectly pathed, etc.

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.

How to find cookies in your browser How to find cookies in your browser Jan 19, 2024 am 09:46 AM

In our daily use of computers and the Internet, we are often exposed to cookies. A cookie is a small text file that saves records of our visits to the website, preferences and other information. This information may be used by the website to better serve us. But sometimes, we need to find cookie information to find the content we want. So how do we find cookies in the browser? First, we need to understand where the cookie exists. in browser

See all articles