Hello,
I have a WP site I am working on that uses AJAX calls to load some view data based on some session data that is set. The problem is, the session data that is set is being emptied after an AJAX call. For example:
- A user logs in, and session data is set to identify that user
- The user goes to a dashboard, where he clicks a link which initiates an AJAX call that loads HTML based on that user's ID (which is in the session)
- The loaded HTML is incorrect because the session data is emptied before the AJAX function is ran
- Thereafter, if you reload the page you are on, the session data is emptied here as well, so the user will have to log in again, where he will again get logged out if he initiates the AJAX call
You may at first think it's a code issue, but I think not. I developed the site on my local machine running Apache, and everything worked fine. However, after uploading it to a Hostgator subdomain, I started getting this problem.
I've set up a barebones test case for anyone who might know what my problem is, which I'm wanting to say it's some kind of hosting configuration problem of some sort, although I'm not entirely sure, as I've never run into this before. The URL for the test case is at: http://accidentreview.lifthousedesign.com/test-page
This page is a blank page, with a single shortcode (PHP source further down in this post) in the body of the page. It simply empties the session, var_dumps it, then adds data to the session, var_dumps it, then displays a button that initiates an AJAX call. The AJAX call simply var_dumps the session (which will be empty because of this issue, whatever the problem is). Another button is below it, which reloads the page without emptying/adding anything to the session, to illustrate that the AJAX call empties the current session (it overwrites it with the empty session).
Shortcode Source:
<?php
add_shortcode('session_test','session_test');
function session_test()
{
if(!isset($_GET['dont_set']))
{
session_unset();
echo 'session cleared:';
var_dump($_SESSION);
echo '<br />';
$_SESSION['var_1']='val_1';
$_SESSION['var_2']='val_2';
$_SESSION['var_3']=array('arr_val_1','arr_val_2');
echo 'added values to session:';
var_dump($_SESSION);
echo '<br />';
echo 'session id:';
var_dump(session_id());
echo '<br />';
?>
<button id="test-session">Send AJAX Call (see console)</button>
<br />
<button id="reload-dont-set">Load Page without Setting/Emptying Session</button>
<script>
function test_session()
{
$.ajax({
url: '/wp-admin/admin-ajax.php',
data: {
action: 'test-fn',
},
success: function(data){
console.log('ajax data:');
console.log(data);
}
});
}
function reload_dont_set()
{
window.location='/test-page?dont_set';
}
$(function(){
$('#test-session').click(test_session);
$('#reload-dont-set').click(reload_dont_set);
});
</script>
<?
}
else
{
echo 'only dumping session (not clearing or setting anything):';
var_dump($_SESSION);
}
}
AJAX source:
<?php
add_action('wp_ajax_test-fn','casea');
add_action('wp_ajax_nopriv_test-fn','caseb');
function casea()
{
echo 'priv:';
var_dump($_SESSION);
echo 'session id:';
var_dump(session_id());
}
function caseb()
{
echo 'no priv:';
var_dump($_SESSION);
echo 'session id:';
var_dump(session_id());
}
Anyway, thanks for taking the time to read and please let me know if ya'll can come up with anything I am missing!