Nathan Loisel
Nathan Loisel

Reputation: 1

Php variable not available

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>

Upvotes: 0

Views: 99

Answers (1)

Bayu Dwiyan Satria
Bayu Dwiyan Satria

Reputation: 1058

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}]

Upvotes: 1

Related Questions