WordPress.org

Ready to get started?Download WordPress

Forums

Wierd AJAX/session issue - sessions being emptied on ajax call (6 posts)

  1. nickniebaum
    Member
    Posted 1 year ago #

    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!

  2. bigneno
    Member
    Posted 11 months ago #

    Hi, have you solved this problem? My ajax/$_session script also works OK on localhost, but not on a web server.

  3. nickniebaum
    Member
    Posted 11 months ago #

    I still have not been able to resolve this, actually. I have found that it works on Rackspace, GoDaddy, and my local setup (MAMP), but not with HostGator shared hosting. I have a situation where I'm going to be forced to come up with some sort of solution to this soon, so if I am able to resolve it or find what is causing the issue, I will be sure to post.

  4. sreenath reddy
    Member
    Posted 10 months ago #

    Iam also having the same problem "nickniebaum".did you get the solutions.
    could you please guide me?.Iam using HostGator server.
    I had moved wordpress site from one server to another server.In local server session worked fine after ajax call but in server(HostGator),after the second call to ajax the php session is destroying and site moved to authentication page.
    Is this problem with php.ini settings in server.
    My php.ini settings in server are phpinfo.
    Please give me a solutions.

  5. sreenath reddy
    Member
    Posted 10 months ago #

    We need to change any settings in php.ini file of server.
    My php.ini settings are in the link phpinfo

  6. sreenath reddy
    Member
    Posted 10 months ago #

    Hi nickniebaum i solved the problem.
    I had changed my php.ini settings to register_globals=off and variables_order=GPCS.
    session should start only in one place now it is working fine for me.

Topic Closed

This topic has been closed to new replies.

About this Topic