3

so in my php I have something like this

$_SESSION['opened'] = true;

But It will not be set to true until user will perform some actions with some other html\php pages

So I need on some Ajax function to be able get this session variable. And some PHP sample of function to get variable in form ready for Ajax to get it.

so I need something to AJAX requesting to an action (to some simple php code) which will return a value from $_SESSION.

How to do such thing?

Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
Rella
  • 65,003
  • 109
  • 363
  • 636

2 Answers2

15

Simple jQuery example:

var session;
$.ajaxSetup({cache: false})
$.get('getsession.php', function (data) {
    session = data;
});

And getsession.php:

<?php
session_start();
print json_encode($_SESSION);

You're not required to use jQuery for AJAX, but I highly recommend it.

Edit:

In response to:

I want to be able to tell my JS function what variable I want to get.

You can try this (untested):

var session;
$.ajaxSetup({cache: false})
$.get('getsession.php', {requested: 'foo'}, function (data) {
    session = data;
});

And the PHP:

<?php
session_start();
if (isset($_GET['requested'])) {
    // return requested value
    print $_SESSION[$_GET['requested']];
} else {
    // nothing requested, so return all values
    print json_encode($_SESSION);
}

Have a look at the $.get documentation for a more detailed overview.

Matt
  • 43,482
  • 6
  • 101
  • 102
  • Yes! I love It, but how to get specific session variable? I meen I want to be able to get specific session variable - not all. and I want to be able to tell my JS function what variable I want to get. – Rella May 04 '10 at 12:34
  • Just pass variable name as request parameter so that PHP knows what to return? – BalusC May 04 '10 at 12:38
  • $.get() will cache the result, which isn't good for sessions. See my answer. – Aiden Bell May 04 '10 at 12:41
  • @Ole see my updated answer to request specific variables. I also updated the code so you can use the handy `$.get` syntax without worrying about it caching. – Matt May 04 '10 at 12:43
  • Can a file like 'getsession.php' not be a big security issue? You can easily read the whole session and maybe some sensible data is stored in it – null Apr 20 '15 at 15:28
3

PHP File at http://my.host/response.php

<?php
session_start();
if(isset($_SESSION['opened']))
    echo "true";
?>

Then in HTML, Add jQuery such as:

<script type="text/javascript" src="/path/to/jQuery-x.y.z.js"></script>

Then:

<script type="text/javascript">
    $(document).ready(function(){
        $.ajax({
            url:'/response.php',
            cache:false,
            success:function(data){
                // Do something with the result
                if(data=="true"){
                    $('#mydiv').show();
                }else{
                    $('#mydiv').hide();
                }
            }
        );
     });
</script>

And add to myform.php:

<h1>Some Random HTML</h1>
<div id='mydiv' class="<?php if(isset($_SESSION['opened']) && $_SESSION['opened']) echo "hidden_class";?>">
 ...</div>

As this will give a consistent experience to those without JavaScript. You don't have to be showing/hiding a div. You could do anything really.

Aiden Bell
  • 28,212
  • 4
  • 75
  • 119
  • You need to call `session_start()` to initialize the session data. – Matt May 04 '10 at 12:47
  • 2
    No, I have it set to auto-start in php.ini :P Don't be so pedantic. – Aiden Bell May 04 '10 at 12:48
  • Hey, if you're going to pick at me for cache issues, I'm going to pick back ;) – Matt May 04 '10 at 12:49
  • 1
    Both of our points were legitimate. I just don't think you should assume someone who is new-ish to PHP to have non-default settings enabled. He's probably on a shared host anyway. – Matt May 04 '10 at 12:52
  • Alright keep your shirt on. But id rather debug a session not starting than my asynchronous calls being cached! – Aiden Bell May 04 '10 at 12:54
  • I'd rather debug neither, so I'm glad we brought these issues to light before it was too late. – Matt May 04 '10 at 12:56