How Taobao obtains Cookie analysis across domains

Source: Internet
Author: User
Tags getscript

I recently found a small detail when I used Taobao, so I wrote this article.

After logging on to www.taobao.com, we directly switch to the www.tmall.com domain name and find that "Hello, andyfaces" is displayed at the top of the www.tmall.com homepage. so we will analyze the implementation mechanism here.

First, the user name should be stored in the cookie, so in the taobao.com domain name, you can see that the user name is actually stored in the cookie in firefox, and tmall.com does not store the cookie:

It is certain that the cookie does not allow access from the domain. In both JS and Server applications, how does tmall.com access the cookie under taobao.com?

So I opened tmall.com and used firebug for debugging. I found a request statement like this.

The JS Code on the page is:

 
 
  1. <script>    
  2.         KISSY.getScript("http://www.taobao.com/go/app/tmall/login-api.php?"+Math.random())    
  3.         </script>   

After seeing this, I probably knew how to deal with it. To confirm it, I searched for the KISSY. getScript function code and actually used the JS cross-origin JSONP solution:

 
 
  1. getScript: function(url, success, charset) {    
  2.             var isCSS = RE_CSS.test(url),    
  3.                 node = doc.createElement(isCSS ? 'link' : 'script'),    
  4.                 config = success, error, timeout, timer;    
  5.     
  6.                 node.src = url;    
  7.                 node.async = true;    
  8.     
  9.             scriptOnload(node, function() {    
  10.                     if (timer) {    
  11.                         timer.cancel();    
  12.                         timer = undef;    
  13.                     }    
  14.     
  15.                     S.isFunction(success) && success.call(node);    
  16.     
  17.                     // remove script    
  18.                     if (head && node.parentNode) {    
  19.                         head.removeChild(node);    
  20.                     }    
  21.                 });    
  22.             head.insertBefore(node, head.firstChild);    
  23.       }   

The principle is to dynamically load js through the dynamic create js include, and then determine the onreadystatechange for the bind onload event of the script node or. For details, refer to the processing of the above scriptOnload function. After the js load is complete, use the callback method to execute the success function.

For further confirmation, the $. getScript of Jquery is used to test a test. First, log on to taobao.com and write a test page locally. Run the following statement:

 
 
  1. $.getScript('http://www.taobao.com/go/app/tmall/login-api.php?0.6783450077710154', function(){    
  2.     console.log("the taobao.com cookie object:" + userCookie + " username:" + userCookie._nk_);    
  3. });   

Firbug result:

In fact, the general principle is that, by providing a php request address on the server side of www.taobao.com to obtain all the cookies in the current domain, the php will get the cookie and generate the js Code, that is, the second one shown above. Then, the jsonp method is used to load the js Code in tmall to implement cross-origin access to cookies.

Link: http://www.iteye.com/topic/1000776

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.