0

I have a php application getting x and y position constantly from the server. Based on the value of x and y, it places an image on screen. The page is constantly refreshed every 10 seconds. I want to save the previously obtained x and y value (before refresh) in to x1 and y1 and then store the currently obtained value in x and y (after refresh), so that I can calculate x1-x and y1-y.

<?php
session_start(); 
if(isset($_SESSION['x1']))
 $_SESSION['x1'] = $x;
else
 $_SESSION['x1'] = 0;
$x = //Some method of fetching the x position from server
?>

Problem with the above code is, that during every refresh the value of x1 is replaced with current x, i.e. x1 becomes equal to x. How do I make x1 to retain its value.

Smamatti
3,9313 gold badges34 silver badges44 bronze badges
asked Nov 14, 2011 at 16:12
1
  • Can you better explain what you are trying to accomplish? You're getting x/y position from what server? You're calling this script via AJAX? What do you want it to output? Commented Nov 14, 2011 at 16:16

3 Answers 3

4

The most obvious solution: Put the values into the session at the end of the request.

session_start(); 
if(!isset($_SESSION['x1']))
 $_SESSION['x1'] = 0;
// your code here
$_SESSION['x1'] = $x;
exit; // Not really, just to clarify, that the script should end here
 // or at least its not that important, what comes next
answered Nov 14, 2011 at 16:17
Sign up to request clarification or add additional context in comments.

2 Comments

To build on KingCrunch's answer you'd probably want to register a shutdown function for that function save_session() { // code } register_shutdown_function(save_session);
Aah...that was so obvious....guess I need to take a break....thank you for your help and support
0

x1 should be retaining it's value from each call in the session. You're overwriting it though immediately with $x from what I see here.

If you're posting your $x variable across compare $x to $_SESSION and look at the two values before you overwrite it. Output the $_SESSION['x1'] before you write to it. You should see the previous value in there before you overwrite it.

answered Nov 14, 2011 at 16:17

Comments

0

You need to post a bit more of your code. Based on what you have I don't see how this could accomplish what you want. Let's say the value of $x is 4. You run your query and $x is now 7. You then set the value of $_SESSION['$x1'] to 7 and save it. But after the refresh you are pulling that value of $x1, which is 7, and immediately resetting to $x. Why? It already has the value you wanted, 7, which is the previous value of $x. You are then going to set $x, which is now 7, to whatever number you pull from the server and the previous value is lost forever.

answered Nov 14, 2011 at 16:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.