Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 573
  • Last Modified:

Browser behavior and php and JavaScript (Recognize Behavior)

Hi E's, I create a game, using some different languages, most php and jquery, and I have a little problem with the browsers, because some browsers actually keep the last session and when we open again the browser they open as such the same thing of the last session.
Like I said, I create a game, and always need new pages that do not keep previous information.

So, what I try to get here, is one way to recognize if the page is new or if it is one of the pages keep previous.

Have a solution, if yes, can be in php or in javascript/jquery.

The best regards, JC
0
Pedro Chagas
Asked:
Pedro Chagas
4 Solutions
 
Barry62Commented:
are there times you need to go back to previous pages?  You said this is when you open a new copy of the browser?
0
 
Pedro ChagasWebmasterAuthor Commented:
Hi Barry, no, don't have times to go back to the previous page.
I will try explain better. For example in Firefox, every time I close the browser FF keeps all the pages I left open in the previous session. When I open again FF, the browser open the pages that I don't close in my last session.
What I want detect (php or js) it is if the page are open from the last session of browsers or not.
If I detect the page are open from the last session in the browser, the script will refresh the page, if are open not from the last session of the browser the script run normally.

Please tell me if you understand.

~JC
0
 
Barry62Commented:
Yes, I understand, now.  Try the code below:

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>

This will tell Firefox not to cache the page and the past date in the expires header will ensure  that other browsers will not cache it either.
0
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
Pedro ChagasWebmasterAuthor Commented:
Hi Barry, not work. The behavior still the same.
I check in the net for other solutions about cache, I try this one also:
Header('Cache-Control: no-cache');
Header('Pragma: no-cache');
and not work too. Maybe not have solution....
If not have a solution in php I have to do something in javascript.

~JC
0
 
Barry62Commented:
OK, looking over your explanation, it sounds like whether you are re-opening a tab to your game that was closed with the browser, or opening a new tab to your game, you want a fresh page, right?

In that case, use PHP sessions.  A session only lasts as long as the page is open.  Once the page is closed, the session goes away.  when it is re-opened, a new session is started.

Here's a good tutorial on PHP sessions:

http://www.tizag.com/phpT/phpsessions.php
0
 
Pedro ChagasWebmasterAuthor Commented:
Hi, php sessions sounds a good solution and much more secure.
I don't know if it is well, but I try do something in php session and the result in firefox was peculiar.
I just try working with "session_id()", I try to view the number of the session. The peculiar thing was that FF always present the same session_id number, when I do a refresh (f5) and when I close the browser and reopen again.
Not is from session_id () should I start checking?

~JC
0
 
Barry62Commented:
Could I see the code you are using for your session?  I will try it in firefox to see what it does.
0
 
Pedro ChagasWebmasterAuthor Commented:
Hi, this is the code (I get part of the site you recommend):
<?php
session_start();
$id = session_id();
echo "$id <br>";  
if(isset($_SESSION['views']))
    $_SESSION['views'] = $_SESSION['views']+ 1;
else
    $_SESSION['views'] = 1;

echo "views = ". $_SESSION['views']; 
?>

Open in new window

That code is just a experience for I know how php session work.

I try to see the session id for found some differences between sessions, but in FF that not happening, the id is always the same independently if I close the browser or not (f5).
And the "views" the same thing, always increment independently if I close or nor the browser.

~JC
0
 
Barry62Commented:
Are you using f5 to simulate a user closing and restarting the browser?  If so, that won't give you the correct results.

f5 in Firefox reloads the browser with cached content.  If you want to completely reload the page a new copy for the cache, you need to use ctrl-f5.
0
 
Pedro ChagasWebmasterAuthor Commented:
Hi, I don't did that (ctrl + f5), I do now, and the result is the same, still the same session ID and the variable is always increment "$_SESSION['views']".
Can you try test the code?
Is better I quit, I star to thing in a new solution.

The best regards, JC
0
 
jrm213jrm213Commented:
Hi,

Speficically with firefox, you can set it up so that when you close the browser (or the browser crashes) that it will re-load your previous tabs, with this functionality, it also re-initializes your previous sessions if those sessions are still valid on the server. For example, I can log into Experts-Exchange, go to my windows process list, kill the firefox process, and then reload firefox, tell it to open my previous session and I will still be logged into Experts-Exchange as if the browser had never closed...

I don't think that you have any way to detect that firefox has done that.

The only thing I can think of that might work for you is using server side sessions and setting the session timeout to a really low amount like 60 seconds. As long as your game makes regular requests to the server it should keep the session alive. I believe the default session timeout is 60 minutes.
0
 
Ray PaseurCommented:
I'm not sure this is happening because I can't really see all of the moving parts, but I'll try to explain something that drives developers nuts, but rarely causes any problems in the "real world."

All of the instances of the browser - both windows and tabs - share the same cookie jar.  So when you visit a web site that uses sessions, the site will put a cookie on the browser and the browser will begin returning that cookie with each request.  The server uses the cookie to find and load the $_SESSION array.  So far, so good.

Now open another instance of the browser.  It can be another tab or another window - it won't matter which.  Go back to the same web site that you visited in the first instance of the browser.  Poof!  You're magically logged in, same as the first browser client.  This happens because both instances of the browser returned the same cookie.  The server thinks that your requests came from the same client, and in fact they did!

Now the confusion begins.  Using one instance of the browser, log out as one user and log in as another user.  Logically, you would think that you have two separate logged in users, but because of the way cookies work, both of the browser instances will be sending the most recent cookie.  So you do some things as "user 2" then go back to the "user 1" screen.  It still looks right, but as soon as you load a new page, poof! Suddenly "user 1" has turned into "user 2" and it seems wrong.  But it's not - it's normal behavior in the context of the stateless client/server systems.

To get around this in testing, be sure to close ALL instances of the browser and open a new instance when you want to switch from "user 1" to "user 2" -- then you will see behavior that makes perfect sense.
0
 
Slick812Commented:
greetings   joaochagas, , Your settings that you check has to take into consideration more than One thing, because of your uses of javascript and php, , you did not use php sessions before, and for this that may can help or not, I feel that you MUST have a database (MySQL) that keeps the info-settings for each user and game, You may can generate a Unique ID, for each and every Single browser Open game that starts (the user first signs in, or starts a running the game) - - you place this unique ID in the database column and send that UID to store in javascript -
var UID = '<?php echo $row['uid']; ?>';,
If your browser uses a Cached web page, and you can detect that the old game was Ended (the web page cached page), by comparing a NEW unique ID in PHP and the game javascript UID. But if there is NO web transfer (page refresh or ajax) then the javascript in cached page code, will keep going as if everything is OK, , and you will have your problem.
Sorry I can not suggest a code fix for javascript, without getting some info - confirmation from the PHP server values for that game. As far as I have tried it, it may be a bad idea to do a javascript page refresh again and again, or set pages on server output to not be cached.
Some measure of Time between server requests may also be nessary, depending on how you got this.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now