2

I hava a strange problem. I'm using jquery $.post() to send/recieve vars from a PHP script.

JavaScript:

$("#r_submit").click(function()
{
    $.post("http://" + server + "/msws/", 
    {
        action: "register",
        sub_action: "register_validate"
    },
    function(json) 
    {
        json = $.parseJSON(json);
        alert(json.cell_is_good);
    });
});

PHP:

http://textuploader.com/?p=6&id=S7zDD

Problem:

If I run the code on my pc (WAMP) it works fine, but if I upload it to my server (justhost) then it dosn't keep the session if I alert the session when I create it, it is there, but when I try to get the session later, it's gone, I think it has to do with the fact that the server thinks that the browser was closed, so it destroys the session? Thank you :)

PhearOfRayne
  • 4,990
  • 3
  • 31
  • 44
dj_boy
  • 103
  • 1
  • 3
  • 9

2 Answers2

2

You are starting your session without a session id. For this to work your PHP has to be configured to use transparent session id, which is deactivated by default. So you will have to start the session in the script that has the AJAX-call, and submit the session_id() in the POST request, and start the session in your PHP something like this:

if (!isset($_POST['SID'])) {
    die('{}');
}
session_start($_POST['SID']);

Also I would leave out the host-part of the url you are calling in your $.post to avoid cross-domain ajax call problems (also this most likely will not fix your problem). It will work only on the same server, so you can write the ajax call like this:

$.post("/msws/", //...
crackmigg
  • 5,571
  • 2
  • 30
  • 40
  • I do no think this is the issue. Cookies are sent with ajax request. @rekire asked a good question that has not been answered that would tell us if this is the case or not. Chrome has developer tools too, press F12. – ficuscr Dec 20 '12 at 19:18
  • The javascript is seprate it is a mobile app, it is not on the server – dj_boy Dec 20 '12 at 19:19
  • Where else would the client side JS be...? All the more reason to inspect the request. Though still not sure how that fits if you say it worked locally. – ficuscr Dec 20 '12 at 19:21
  • OK - you said you are seeing the session in the header. Then this answer is probably not your solution. Maybe explore the path scope comments (cross domain). See this post: http://stackoverflow.com/questions/1041285/does-jquery-send-cookies-in-a-post – ficuscr Dec 20 '12 at 19:25
0

check if you have multiple session cookies.

i've the same situation with codeigniter and i found that every new page creates its own session

Hager Aly
  • 1,113
  • 9
  • 25