0

I use JQuery Ajax to call sessions variables from PHP steamauth file, but it's not working, with this code (bellow), when I replace "$data['test'] = $_SESSION['steamid'];" to "$data['test'] = "ok";", it works very well, but with the initial code, no any alert window is poping and the div (ok), is still set to '...'.

I have no idea why I can't get the $_SESSION['steamid'] variable if it is set. Thank you very much

test.php:

<?php

header('Content-Type: application/json');
require ('steamauth/steamauth.php');
$data = array();
if(!isset($_SESSION['steamid'])) {
    $data['retour'] = "ok";
    $data['test'] = $_SESSION['steamid'];
    echo json_encode($data);
} else {
    include ('steamauth/userInfo.php');
    $data['retour'] = "not ok";
    echo json_encode($data);
}      
?>

And main.html:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $.get("test.php", function(data, status){
            alert(data.retour);
            document.getElementById('ok').innerHTML = data.test;
        });
    });
});
</script>
</head>
<body>

<button>clic</button>
<div id='ok'>...</div>
</body>
</html>
4
  • 4
    Your if is !isset( - so your saying if this field isn't set then try and use it. Commented Oct 31, 2018 at 11:23
  • Make sure $_SESSION['steamid'] is set. Commented Oct 31, 2018 at 11:24
  • 1.in test.php after <?php use session_start(); 2. if !isset($_SESSION['steamid'] condition is true then $_SESSION['steamid']is not available so how can you assign this to another variable? Commented Oct 31, 2018 at 11:24
  • so basically, the ! shouldn't be there and is what is stopping it working. I'm voting to close this as a typo. Commented Oct 31, 2018 at 11:47

1 Answer 1

1

First you need to start session in test.php

<?php

session_start(); //HERE SESSION IS STARTED

header('Content-Type: application/json');
require ('steamauth/steamauth.php');
$data = array();
$_SESSION['steamed']='WHAT EVER YOU WANT';
if(!isset($_SESSION['steamid'])) {
    $data['retour'] = "ok";
    $data['test'] = $_SESSION['steamid'];
    echo json_encode($data);
} else {
    include ('steamauth/userInfo.php');
    $data['retour'] = "not ok";
    echo json_encode($data);
} 
?>

Output should be

[{retour: ok},{test: WHAT EVER YOU WANT}]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.