I have the problem that my $_SESSION
array is empty on the ajax call although I have set a $_SESSION
variable on the beginning of the page.
For some reason the $_SESSION
array is __not__ empty when I refresh the page or create another ajax file. I do not understand what is happening here.
Here is my minimal example:
test.php
<?php
include 'sessionFile.php';
print_r($_SESSION);
?><!DOCTYPE html>
<html>
<head>
</head>
<body >
<button id="myButt">Click</button>
<div id='memberSearchContent'> </div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#myButt').click(function(){
request = $.ajax({
url: "ajax.php",
type: "post",
dataType: "html"
});
// Callback handler that will be called on success
request.done(function (data, textStatus, jqXHR){
$('#memberSearchContent').html(data);
});
});
});
</script>
</html>
sessionFile.php
<?php
session_start();
$_SESSION['lang'] = "3";
ajax.php
<?php
session_start();
print_r($_SESSION);
When I load test.php and press "Click" then the ajax.php returns an empty array. Here is a screen shot:
If I keep pressing "Click" again, then I get still the same result.
Now to my surprise, when I press "F5" and reload the page, and then press "Click", then the array is no longer empty. Here is a screen shot:
If I close the browser, reopen it, and press "Click", then the array is empty again.
Another thing that I noticed is the following: If I create a new file sessionFile2.php and copy the content from sessionFile.php into it, and include sessionFile2.php , then surprisingly the ajax array is never empty. If I then include again sessionFile.php instead of sessionFile2.php I have the same old error. This does only work when I create a new file and copy its content. If I only include a copy of the file sessionFile.php then the $_SESSION
array is again empty. I don't know why this is the case, since both files have the same owner and same chmod.
I would be really happy if someone could help me out what is going on here.