0

Bottom Line Up Front: Is there a way to update session variables from a script on the server running outside the session?

Detail: I am using MySQL to store large amounts of user information. When a user logs in, I store some of that information in $_SESSION, as a way of caching the data in order to cut down on the number of SELECT queries. The user works with the data, and any changes made are saved to the database.

The problem is that the data on the database could change while the user is working with the $_SESSION representation of the data. In that case, one set of changes could overwrite the other. It's not feasible to 'lock' the data in the database while it is being used in $_SESSION.

It would be fairly straightforward to flag the database with a $_SESSION id when the data is 'checked out' and then update the $_SESSION representation of the data, if a script makes changes to the checked out data. I just don't know what (if anything) can be called from a script on the server called outside the session to change the variables within that session.

3
  • I assume you are storing user data in session while user login, right? Commented Mar 30, 2014 at 10:15
  • Then when you update mysql data in the mean same time you can update session data too. is it make sense? Commented Mar 30, 2014 at 10:23
  • Yes, that's exactly what I'm looking to do. I just don't know how to edit variables in a specific session from the server, instead of from a page being loaded by the user. Commented Mar 30, 2014 at 10:26

3 Answers 3

3

After getting your comment, I think this solution will work for you:

when you start session, keep session id in you database: i.e

<?php
session_start();
$sid = session_id(); // suppose o4lb6s392r2mj1vv6nhlrjfmp1
$_SESSION['user_solr'] = 'admin';

you will get a string. when you want alter session data, without refresh user page load, do following to change user's session data:

<?php
session_id('o4lb6s392r2mj1vv6nhlrjfmp1'); // previous session id
session_start();
$_SESSION['user_solr'] = 'editor';

Updated: This will also work:

<?php
session_start('o4lb6s392r2mj1vv6nhlrjfmp1'); // previous session id
$_SESSION['user_solr'] = 'editor';
Sign up to request clarification or add additional context in comments.

Comments

1

Store your session in database and use SQL trigger to update the session table. That way when the user table is changed, the sql trigger will update the session table also.

You can read more and see some examples in MySQL documentation

Comments

0

What you could do is make an Ajax request to the server every so often, which checks whether the session variables have been updated:

place the $_SESSION variables inside of a hidden input, and send it towards the server.

your php/html

<form id='formid'>
<input type='hidden' name='name' value='<?php echo $_SESSION['name']; ?>'>
</form>

the JS

window.setInterval($("#formid").submit());

$("#formid").submit(function(){
   $.ajax({
      type: "POST",
      url: "someFileToUpdateTheSession.php",
      data: $(this).serialize(),
      success: function(){
          // Do what you want to do when the session has been updated
      }
   });

   return false;
});

Other php file

<?php
   session_start();
   $_SESSION["name"] = $_POST["name"];
   // etc.
?>

Then you just check if the $_SESSION variables have changed, and if so, you update the $_SESSION variables.

3 Comments

I wonder if this defeats the purpose. I could always just reload the data out of the database every time the user loads a page, if I wanted to. My hope was to cut down on database queries. Wouldn't this require the server to check the $_SESSION vars against what's in the database?
I was looking for something where the script that updates the database on the server grabbed the sessionID of the session that has the data 'checked out' (I don't mind saving that to the database while the user is logged in) and then updates those session variables without the user ever knowing (since they live on the server anyway).
You could also do that using Ajax calls, the second PHP file is where you launch anything, this is just a small tactic to check the users SESSION vs the server in the background. Otherwise, I'm not really sure how you could achieve such a thing, sorry.

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.