Connecting Tech Pros Worldwide Help | Site Map

Game Site

Member
 
Join Date: Dec 2007
Posts: 47
#1: Jun 29 '09
Hello all,

I've been slowly creating a text-based game site for a while now when I have time....

The site has a lot of JavaScript and usually downloads info from the MYSQL database on page load then uses equations to predict what the server value will be after a certain time...

This approach may cause problems when if the user logs in on multiple browsers as they will be able to cause changes in their account but the other browsers will not have detected this unless they are refreshed.

My question is how could i stop the user logging in on multiple browsers? I was thinking of something like a random variable is generated on login. Stored in a JavaScript variable and in the database. all interactions need this and if they then log in on another browser the variable on the server is updated so the new window can access the server but now the old browser has wrong code and can not.

The only problem with that is it will only show as being invalid on an action from the user on the old browser. Would be nice if there was a way for it to pop up on the old browser that he/she has been logged out due to logging in on the new browser as it happens.

:D

Probably impossible?

I also understand it’s debatable whether this is actually a MYSQL question.

Thanks in advance for any insight.
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,733
#2: Jun 29 '09

re: Game Site


Hi.

You could avoid this problem altogether by firing AJAX calls every few seconds (or whatever) to check if there have been any updates.
Simply record the time whenever you update the info in the database, have AJAX fetch that time every now and then and compare the new time with the time when the page, or last update, was loaded.
If the time is newer, have AJAX fetch the updated data and update the page.

If you want to restrict the user to a single login in a single browser you could do what you suggested; generate a random key that the client must pass whenever an update is requested.
Then you could have AJAX pass the key to the server every now and then (perhaps with the status updates), and have the server validate it. If it fails validation, the client is denied access to the server.
Member
 
Join Date: Dec 2007
Posts: 47
#3: Jun 30 '09

re: Game Site


Yeah, I'm just very wary of using too many AJAX requests as if there are a few logged in it may cause load issues. I could send the key perhaps every 2 mins with the time resync.

What are your thoughts on this code? Its my main script and it page has 3 functions called getPage.. and the template files has similar. The idea being that each page would have had a bulk of code (now in template) that was duplicated this way template script is only in one file.

I could probably do that check somewhere in here but this is only executed on page refresh. :S

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. ob_start(); //Buffer all output.
  3. session_start();//Start global session.
  4.  
  5. require("PHP/TemplatePHP.php"); //Website template
  6. include("PHP/dbCon/config.php"); //Required for db connection
  7. include("PHP/dbCon/opendb.php"); //Required for db connection
  8.  
  9.  
  10.  
  11. //Check if user is logged in.
  12.  
  13.  
  14. if(isset($_GET["page"])){$inPage="PHP/". $_GET["page"] .".php";}
  15. else{$inPage=null;}
  16.  
  17. if(!isset($_SESSION['username']))
  18. {
  19.     if($inPage == "PHP/Register.php")
  20.     {
  21.         $outPage = $inPage;
  22.     }
  23.     else
  24.     {
  25.         $outPage = "PHP/Login.php";
  26.     }
  27. }
  28. else
  29. {
  30.     if(file_exists($inPage))
  31.     {
  32.         $outPage = $inPage;
  33.     }
  34.     else
  35.     {
  36.         $outPage = "PHP/Error.php";
  37.     }
  38. }
  39.  
  40. include($outPage);
  41.  
  42. tempPageP1(true);
  43.  
  44. //Header additions (JS)
  45. getPageJS();
  46. //Header additions (CSS)
  47. getPageCSS();
  48.  
  49. tempPageP2(true);
  50.  
  51. //Page Title
  52. getPageTitle();
  53.  
  54. tempPageP3(true);
  55.  
  56. //Message to user.
  57. if(isset($_SESSION['message']))
  58. {
  59.     echo "<p id=\"userMessage\">{$_SESSION['message']}</p>";
  60.     unset($_SESSION['message']);
  61. }
  62. //Page body
  63. getPageContent();
  64.  
  65. tempPageP4(true);
  66.  
  67. include("PHP/dbCon/closedb.php");
  68. ?>
Reply


Similar MySQL Database bytes