0

I have a $_SESSION created in php. One of its values is the last activity stamp. I'd like to show the stamp on the <div> element on the page.

The $_SESSION['LAST_ACTIVITY'] is being updated in php part of the code, and according to the console output, I can see it is changed correctly:

<?php
$action = $_POST['action'];
switch ($action) {
    case 'login':  DoLogin(); break;
    case 'logout': DoLogout(); break;
    case 'update': updateSessionActivity(); break;
    default: $errmsg_arr[] = 'Unknown action'; $errflag = true;
}

function updateSessionActivity()
{
    if (isset($_SESSION['LAST_ACTIVITY'])) {
        $_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp
    }
    echo "Session updated: ".$_SESSION['LAST_ACTIVITY'];
}
?>

The previous php function is being called from jQuery with $ajax when switching between the nav pane tabs, posting to php page and getting the $_SESSION parameter updated (I can see it in network response correct after echo Session updated).

However the <div> element's html with ID "page_header_cont" gets updated only once (first time I switch to another tab pane) and never changes after, although I continue to switch between the tabs and see that POST works correctly with variable updated.

<div id="page_header_cont" class="page_header_cont">
</div>

<script type="text/javascript">
    $("a[data-toggle='tab']").on("shown.bs.tab", function (e) {
        $.ajax({
                url: 'loginprocess.php',
                type: 'POST',
                data: {action: 'update'},
                success: function (response) {
                    $("#page_header_cont").html(" Last activity: " + "<?php echo date('Y-m-d H:i:s',$_SESSION['LAST_ACTIVITY']) ?>");
                },
                fail: function (response) {}
        });
    });
</script>

What am I missing, why the php echo returns the same value for the variable? Tried in several browsers, thought it is some sort of cache issue, even tried to echo time(). The same output. First time works correctly, then ceases to update.

Edit I was asked to edit the question to specify how it differs from what supposed to be the same questions. I would like to point at the fact that the answer and question provided as duplicate are way too differ from the problem in my question. I do have php code executed while surfing on the page and I DO have my variables changed. Now after the answer I have them also changed on the front page and they are not "hardcoded", whatever that means.

  • Look at View Source. – SLaks Nov 12 '17 at 14:33
  • Actually, that's the first thing I did to see I have a problem. The source shows the value that was first received and after never changes. – Mikhail Ushakov Nov 12 '17 at 14:38
  • See also https://stackoverflow.com/questions/12498839/how-to-execute-php-code-within-javascript, https://stackoverflow.com/questions/7165395/call-php-function-from-javascript, https://stackoverflow.com/questions/8471945/how-can-you-use-php-in-a-javascript-function etc etc. The short answer to `why the php echo returns the same value for the variable?` is that it doesn't, PHP is evaluated *once* on the server and the output is hard-coded into your page forever after. – user229044 Nov 12 '17 at 14:41
  • I was writting an answer but the topic is locked now. But the TL;DR; is this: Make your PHP return an JSON using `json_encode()`, read it on you `success` option for `ajax()` (your response will now be an object). – Elias Soares Nov 12 '17 at 14:49
  • meagar, you are actually wrong, since I clearly stated that PHP I evaluate each time I switch tabs. Elias, PHP is returning the string and it works correct. Anyways, the question has been answered and it helped. – Mikhail Ushakov Nov 12 '17 at 14:56

1 Answers1

0

This line won't change between ajax calls:

$("#page_header_cont").html(" Last activity: " + "<?php echo date('Y-m-d H:i:s',$_SESSION['LAST_ACTIVITY']) ?>"

You should replace the php code by the data you receive in the ajax response.

To set an initial value, you can "write" a javascript variable and take the value from there in $(document).ready();

To do this, at the bottom of the body, you can set:

<script>
var myPhpVrs = {"lastActiviy" : "<?php echo date('Y-m-d H:i:s',$_SESSION['LAST_ACTIVITY']) ?>"};
</script>

Then you can get the initial value from myPhpVrs.lastActivity, and any update from the ajax response.

Aditional note: Consder escaping php code to avoid javascript injection.

Juan
  • 5,525
  • 2
  • 15
  • 26
  • The first part of your answer worked perfectly. I had to change only the output for the php function to return the formatted string as a response. About the initial value it wasn't a problem though, since the first initialization of the $_SESSION parameters was returning the correct values. Do you still suggest to stick with JS variable for that? – Mikhail Ushakov Nov 12 '17 at 14:48
  • Perhaps this needs more thinking, but I prefer to download data in variables from PHP to javascript instead of hardcoding php outputs into javascript functions. – Juan Nov 12 '17 at 14:54
  • Juan, thank you very much. Marked as an answer, sorry can't upvote yet – Mikhail Ushakov Nov 12 '17 at 14:59