Solved

Browser behavior and php and JavaScript (Recognize Behavior)

Posted on 2013-01-10
15
546 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
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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 108

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 33

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now