Home >Backend Development >PHP Tutorial >After session sharing is allowed, the ajax request generates a new session_id

After session sharing is allowed, the ajax request generates a new session_id

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-10-11 14:23:101262browse

The current situation is:
The session_id generated by directly accessing cd.xxx.com is the same as the session_id generated by accessing www.xxx.com
But using ajax in cd.xxx.com to request www.xxx.com will generate a new one session_id causes verification failure!
Is there any way to deal with it? Thanks in advance!

Reply content:

The current situation is:
The session_id generated by directly accessing cd.xxx.com is the same as the session_id generated by accessing www.xxx.com
But using ajax in cd.xxx.com to request www.xxx.com will generate a new one session_id causes verification failure!
Is there any way to deal with it? Thanks in advance!

This problem belongs to the problem of Ajax carrying cookies across domains. I found a solution in a blog post.

Native ajax request method:

<code>var xhr = new XMLHttpRequest();  
xhr.open("POST", "http://xxxx.com/demo/b/index.php", true);  
xhr.withCredentials = true; //支持跨域发送cookies
xhr.send();</code>

jquery post method request:

<code> $.ajax({
    type: "POST",
    url: "http://xxx.com/api/test",
    dataType: 'jsonp',
    xhrFields: {withCredentials: true},
    crossDomain: true,
})</code>

Server side settings:

<code>header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: http://www.xxx.com");</code>

Thanks to the original author:
http://blog.sina.com.cn/s/blo...

Statement:
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