ajax-based login fails: IE6 and IE7 ajax calls delete session data (session_id however is kept)
This question comes after two days of testing and debugging, right after the shock I had seeing that none of the websites i build using ajax-based login work in IE<8
The most simplified scenario si this:
1. mypage.php :
session_start();
$_SESSION['mytest'] = 'x';
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
</script>
<script type="text/javascript">
function loadit() {
$.post('http://www.mysite.com/myajax.php', {action: 'test'}, function(result){alert(result);}, 'html');
}
</script>
<a href="javascript:void(0);" onclick=开发者_运维问答"loadit(); return false;">test link</a>
2. myajax.php
session_start();
print_r($_SESSION);
print session_id();
When I click the "test link", the ajax call is made and the result is alert()-ed:
IE6:
weird bullet-character (•)
IE7:
Array(
)
<session_id>
IE8/FF (Expected behaviour):
Array(
[mytest] => 'x'
)
<session_id>
I would really appreciate some pointers regarding: 1. why this happens 2. how to fix it
Thank you.
Make sure IE isn't caching the response from your request, put this before your post calls run:
$.ajaxSetup({ cache: false });
May seem like a dumb Question, however have you verified that your session_start() has absolutely no whitespace in either of the scripts? this will generally give an error...
Try using relative urls in the ajax request. Also, in ie6/7 are you going to 'mysite.com' or 'www.mysite.com'. Check that the host header is correct in the request.
$.post('myajax.php', {action: 'test'}, function(result){alert(result);}, 'html');
Could it be something to do with text encoding? Are you using UTF-8
(or whatever encoding) in both the main page and the ajax script? If so, make sure you are explicitely telling the browser which encoding to use in the response headers and HTML head.
It might just be that the print_r()
happens to output some magic combination of characters that makes IE detect the wrong encoding.
Also try header('Content-type: text/plain');
perhaps, to make sure IE doesn't try to interpret it as HTML.
One more thing to try: Is IE passing the cookie correctly through the ajax request? Is the Session ID the same for the main page and ajax script? If the cookie is not being passed, PHP might be starting a new, separate session for the ajax page.
精彩评论