Solved

Browser behavior and php and JavaScript (Recognize Behavior)

Posted on 2013-01-10
15
562 Views
Last Modified: 2013-01-22
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
Comment
Question by:Pedro Chagas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
15 Comments
 
LVL 8

Expert Comment

by:Barry62
ID: 38764563
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
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 38764790
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
 
LVL 8

Expert Comment

by:Barry62
ID: 38765596
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 3

Author Comment

by:Pedro Chagas
ID: 38766563
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
 
LVL 8

Assisted Solution

by:Barry62
Barry62 earned 100 total points
ID: 38767725
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
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 38773127
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
 
LVL 8

Expert Comment

by:Barry62
ID: 38774534
Could I see the code you are using for your session?  I will try it in firefox to see what it does.
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 38774756
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
 
LVL 8

Expert Comment

by:Barry62
ID: 38775794
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
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 38786703
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
 
LVL 17

Assisted Solution

by:jrm213jrm213
jrm213jrm213 earned 110 total points
ID: 38801491
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
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 110 total points
ID: 38801512
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
 
LVL 34

Accepted Solution

by:
Slick812 earned 180 total points
ID: 38802855
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

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
The viewer will learn how to count occurrences of each item in an array.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question