0

When I try to check is $_SESSION['logger'] started I get empty answer. Before when I used only php it worked but now when I tried with juqery it does not.

Let say my code on html is:

<input name="logger" id="logger" />
<input name="submit" id="submit" type="submit" value="Submit">

And jquery code is:

$("#submit").click(function(){

var logger = $("#logger").val();


   $.ajax({

    type: "POST",
    url: "login.php",
    data: {logger: logger},
    success: function(logg){

    window.location.replace(logg);

    }

});

});

And php is :

session_start();
if($_POST['logger']!="" && $_POST['logger']!=NULL){

NOW I NEED TO CHECK IS $_POST['logger']==$_SESSION['logger']

}

But it seems I can't get $_SESSION val because it was never started.. How can I start session with jquery by pressing button.... If you know what I mean. I need this for logged users if $_SESSION['logger'] is set to redirect to his profile from index page..

stg
  • 19
  • 4
  • why is this duplicate when it is different question – stg May 12 '16 at 13:41
  • *Why* do you need to check their login status with jQuery? If your goal is to have them go to the profile page, you can do that entirely with PHP. So far, this sounds like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem/66378#66378) – Adam Konieska May 12 '16 at 13:41
  • @stg if your question essentially is "how do I access a PHP variable with jQuery" the answer is the same - you can't. – Adam Konieska May 12 '16 at 13:44
  • I know i can with php i asked can I with jquery – stg May 12 '16 at 13:44
  • If you place in both files `session_start();` you can simply check for `if(isset($_SESSION['logger']))`... – Jeroen Bellemans May 12 '16 at 13:51

1 Answers1

0

I think you might be confusing at what point a session is created, and what that means exactly. If you want to start the session earlier, just serve the HTML from a PHP file, with session_start(); in it.

That being said: I think the main issue here is that your ajax request might not be send the session cookie with it. The first step would be verifying that with the Chrome Developer Tools.

Daan Meijer
  • 1,324
  • 7
  • 12