我想通过使用会话创建一个计数器。但是如果我通过Ajax调用计数器,则会话总是被重置。怎么了?
这是我的PHP文件:
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Credentials", "true");
header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
header($_SERVER["SERVER_PROTOCOL"]." 200 Ok");
if( !isset( $_SESSION ) ) {
session_start();
};
if (!isset($_SESSION['zaehler'])) {
$_SESSION['zaehler'] = 1;
} else {
$_SESSION['zaehler']++;
};
echo "Zähler: ".$_SESSION['zaehler'];
我的Jquery:
$.ajax({
type: "GET",
data: { whatever:'cool' },
url: "http://www.huntinggrounds.de/test.php",
xhrFields: { withCredentials: true }, //by trying this I will get a cors error by browser.
crossDomain: true,
success: function(response) {
console.log(response);
},
beforeSend: function(xhr){
xhr.withCredentials = true;
}
});
通过调用文件test.php,它会计数。通过ajax调用它始终以1开始。只需单击窗口并查看控制台,即可查看Ajax Testfile。
任何想法? THX提前提示任何提示。