简体   繁体   中英

Get a PHP $_SESSION variable from Jquery?

I'm not yet a JSON/AJAX master so I don't know how to do this.

I need a $_SESSION['name'] PHP variable to work with in my jQuery stuff and I don't know how to access it... consider:

// the 'who is typing' shindig
    $.ajax(
    {
        url: "whos_typing.html",
        cache: false,
        success: function(whos)
        {   
                // here I need to access $_SESSION['name'] and do stuff with it

            $("#soandso").html(whos); //Insert who's typing into the #soandso       
        }
        });

You'll need to inject it, something like this:

var sessName = '<?php echo $_SESSION['name']?>';

The file containing this script must be executed by the php interpreter (ie a .php file)

EDIT: Conceding to Radu's point, it would be safer execute for unsanitized data:

var sessName = <?php echo json_encode($_SESSION['name']) ?>;

You need to use $.post to retrieve the variable from the server. You would have something like this:

 $.post('echoMyVar.php', {post: 1}, function(data){
      myVar = data['myVar'];
      });

This is very basic, you first need to check if data is not null. In echoMyVar.php, you need just need basically the following:

 header('Content: application/json', 1);

 $returnVal = array('myVar', $_SESSION['myVar']);

 echo json_encode($returnVal);

Again this is a shell, not secure, and would not handle any errors.

var name= "<?php echo $_SESSION['user_name'];?>"

will do it . . .

Remember php is a server side script, . . .so it takes precedence and get executed first and spits html to the client (Jquery , javacript) which will be executed in your browser . . . .

So, you can use server side variables to share with client . . . but not the other way around . . .

The easiest way is probably to include your javascript code in a .php file. Then you can simply do:

var phpVar = <?php echo $_SESSION['name']; ?>

SIMILAR POST

Server side in whos_typing.php :

<?php
//...
header('Content-Type: application/json');
echo json_encode(array(
    'who'=>'Bob',
    'session'=>$_SESSION,
    // Be sure that you're not storing any sensitive data in $_SESSION.
    // Better is to create an array with the data you need on client side:
    // 'session'=>array('user_id'=>$_SESSION['user_id'], /*etc.*/),
));
exit(0);

Client side:

// the 'who is typing' shindig
$.ajax({
    url: "whos_typing.php",
    dataType: 'json',
    cache: false,
    success: function(data) {
        var session = data.session,
            who = data.who;
            console.log(session.user_id); // deal with session
        $("#soandso").html(who); //Insert who's typing into the #soandso
    }
});

You need to echo the session variable from PHP when you send it to the browser. I'm assuming whos_typing.html is just the URL to a PHP script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM