/    Sign up×
Community /Pin to ProfileBookmark

In php is there a method for that can be used when a user closes the browser?
I want to set a variable when the user closes the window

Thanks

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@BrewsterMar 12.2007 — Hi

There is no method for detecting whether the user has closed the windows in PHP as PHP works on the server.

What you may be able to do is launch a popup window from javascript when the user closes the window.

What are you trying to do ?

Brew
Copy linkTweet thisAlerts:
@bunny1authorMar 12.2007 — wanted to set a session variable so that if a user logs out or closes the browser the variable is reset and they must re-login.

thanks!
Copy linkTweet thisAlerts:
@NanscombeMar 12.2007 — Hi Bunny1,

If movement through your site is by using Hyperlinks, how about also passing a variable through the URL? and comparing it with a $_SESSION variable held on the Host.

... mydomain.com/index.php?SID=12345

If SID doesn't exist or if the URL code SID did not match the $_SESSION SID, then create a new one.

How about something like that?

Regards

Nigel
Copy linkTweet thisAlerts:
@bunny1authorMar 12.2007 — if a user bookmarks the page will they be able to access it?It's for a CMS so i want it to be secure.I dont want someone being able to access any page without logging in.

thank you!
Copy linkTweet thisAlerts:
@NanscombeMar 12.2007 — Hi Bunny1,

Hmmm, not sure. I'm just trying to come up with something off the top of my head. ?

What you could do is, when a user first starts a session create a session variable [B]SID[/B] (session ID) and a second one, say [B]SID2[/B] which would be blank.

On loading a secure page attempt to set [B]SID2[/B] to the value passed through the URL.

If [B]SID2[/B] is not equal to [B]SID[/B] or even better if [B]SID2[/B] is not equal to the MD5 of [B]SID[/B] (one way encrypted version of [B]SID[/B]) assume that the user is not logged in and redirect them to the login page.

This will happen on the first page because there will be no value in [B]SID2[/B] initially.

This would also happen with a bookmarked page because the two values would not match.

Once the User is successfully logged in, include the MD5'd (encrypted) version of [B]SID[/B] in the URL as [B]?SID2=MD5'd(SID)[/B].

This time when they go to a page, [B]SID2[/B] should equal [B]MD5(SID)[/B] so they would be logged in.

That would be the crux of my idea.

Alternatively, a simpler method, just set an expiring [B]LoggedIn[/B] cookie and check for that. If cookie expired then Log In.

Regards

Nigel
Copy linkTweet thisAlerts:
@TaschenMar 12.2007 — Session IDs are set to automatically end when a user leaves your site. This means that if a user has finished viewing a page and they navigate away from the page, the session will expire (unless told to do otherwise in PHP ini settings, and dependent on whether the session_id is set as a cookie or in the URL). If you are trying to build a CMS I would recommend reading the PHP.net manual (for sessions try www.php.net/sessions).

However, as a general rule it is not a good idea to simply set a true or false token and allowing access if the token is presesnt. The token needs to be evaluated and permission granted depending on this evaluation. Even better, grant a one time, time limited token for each action.
Copy linkTweet thisAlerts:
@NanscombeMar 12.2007 — Hi Taschen,

You raise some interesting points. ?

Building on my original idea, if I ever have to do this for real, it may be prudent to generate [B]SID2[/B] from [B]MD5(SID+OneOffKeyPerPage)[/B]. That, in theory, should make it more secure. ?

Nigel
Copy linkTweet thisAlerts:
@TaschenMar 12.2007 — Hi Taschen,

You raise some interesting points. ?

Nigel[/QUOTE]


Glad you think so!

As you identitified evaluating one SESSION value against another is important. A basic flow is something along the lines of:

  • 1. Set a session_id as a cookie to maintain page state. (session_id can available in the URL or as a cookie).


  • 2. User logins in, if succesful set a unique session_id[user] identifier and link the identifier to your user. Mark the login time in the db. When you set this user id, use an algorithm where the parameters are hard to determine [e.g. the id shouldn't be character value of user name X todays date that's too easy to work out]. Once you have your session_id[user] value, md5 hash it.


  • Now you have a means to identify your user which is linked to a user but isn't the users name/password, it is a value that is created (changes) at each login, and the method of creation isn't obvious.

  • 3. When a user attempts to load a page, send a form, perform an action: does the session_id[user] exist -> yes, evaluate the session_id[user] value -> if the value exists -> evaluate if the time stamp is within a predefined limit -> perform action.


  • Using a similar model you could set different permissions on an action for different users.
    ×

    Success!

    Help @bunny1 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...