SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Zealot kemp's Avatar
    Join Date
    Aug 2004
    Location
    Campinas
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Session - How to know when it ends?

    How can I know when a session has ended without the use of "session_unset"? (for exemple when a user close the current window but doesn't go to the log-out page)

  2. #2
    Put your best practices away. The New Guy's Avatar
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    2,087
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Mabye something like this:

    PHP Code:
             if(!session_id()){
                     
    // No Session
                 
    session_start();
             }
             else{
                     
    // Already a session
             

    "A nerd who gets contacts
    and a trendy hair cut is still a nerd"

    - Stephen Colbert on Apple Users

  3. #3
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    When a user closes a window (and no other windows are open) a session ends. They don't have to go to the logout page for it to end.

  4. #4
    SitePoint Zealot kemp's Avatar
    Join Date
    Aug 2004
    Location
    Campinas
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But how can I use this information (session ending) to update something on a table database?

  5. #5
    <? james('rules'); ?>
    Join Date
    Jun 2004
    Location
    Wales, UK
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Assuming you have a $_SESSION set..
    PHP Code:

    if(!isset($_SESSION['whatever'])) {

    // connect to the database then

    $query "UPDATE table SET blah='blah' WHERE blah='blah'";

    $insert mysql_query($query)or die(mysql_error());


    If knowledge is power - Why isn't our army librarians?!
    Statistics show that 63% of all statistics are fake.
    When i was little i broke my neck, and i havent looked back since .
    I completed the internet in 1 week. The end boss was pretty easy though .

  6. #6
    SitePoint Zealot kemp's Avatar
    Join Date
    Aug 2004
    Location
    Campinas
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But this way I can't know info from the session.
    Let me explain better what I want. I wanna make a users online list. I was thinking of updating a users online table when someone logs in. But then, how can I update when he logs out, the way I said above?

  7. #7
    SitePoint Wizard subnet_rx's Avatar
    Join Date
    Aug 2001
    Location
    Hattiesburg, MS
    Posts
    1,085
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought they did it with cron jobs.

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    on every page refreshing u see how many logged user u have!
    if u want to see it in real time u have to take some php online users script and go to javascript session and then ask there how to update in real time a number with this script.

  9. #9
    SitePoint Zealot kemp's Avatar
    Join Date
    Aug 2004
    Location
    Campinas
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by reminder
    on every page refreshing u see how many logged user u have!
    if u want to see it in real time u have to take some php online users script and go to javascript session and then ask there how to update in real time a number with this script.
    But there is no way for me to delete a user from the online users table when he logs out without hitting the Logout button, right?

    How can I see the sessions created on the server and check which one is gone?

    I'm kinda confused

  10. #10
    Put your best practices away. The New Guy's Avatar
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    2,087
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Ah, there is no way to do that. What things like Vbulliten do is periodically check to see if the user is online.

    So in steps:

    1. Add user and date/time of access to DB.
    2. Everytime the user performs an action update the date/time.
    3. Have a regular function to test if the date/time is older than a give value. Say an hour. If it is delete user from DB. He is no longer online.
    "A nerd who gets contacts
    and a trendy hair cut is still a nerd"

    - Stephen Colbert on Apple Users

  11. #11
    SitePoint Zealot kemp's Avatar
    Join Date
    Aug 2004
    Location
    Campinas
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking

    Quote Originally Posted by The New Guy
    Ah, there is no way to do that. What things like Vbulliten do is periodically check to see if the user is online.

    So in steps:

    1. Add user and date/time of access to DB.
    2. Everytime the user performs an action update the date/time.
    3. Have a regular function to test if the date/time is older than a give value. Say an hour. If it is delete user from DB. He is no longer online.
    Yeah, I didn't think it was possible but I had to ask

    Thanks!

  12. #12
    <? james('rules'); ?>
    Join Date
    Jun 2004
    Location
    Wales, UK
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Logout.php:
    PHP Code:
    session_start();

    $destroy session_destroy();

    if(
    $destroy) {

    $query "UPDATE users SET online='no' WHERE id='blah' and online='yes'";

    $update mysql_query($query)or die(mysql_error());

    header("Location: wherever");


    That could be a logout script. How to do what you do with when the browser closes etc, no, that isnt possible .
    If knowledge is power - Why isn't our army librarians?!
    Statistics show that 63% of all statistics are fake.
    When i was little i broke my neck, and i havent looked back since .
    I completed the internet in 1 week. The end boss was pretty easy though .

  13. #13
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I think he is wanting to know how he could do a logout when the browser is closed.

    You could try some fancy Javascript and see if that works. When a user closes a window on your site, you open a very small one up, run your PHP and then close that window. It could work.

  14. #14
    o_O O_o BlueFire2k5's Avatar
    Join Date
    Mar 2003
    Location
    Sioux Falls, SD
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Hartmann
    Yeah, I think he is wanting to know how he could do a logout when the browser is closed.

    You could try some fancy Javascript and see if that works. When a user closes a window on your site, you open a very small one up, run your PHP and then close that window. It could work.

    However, that very well could be taken as a popup and killed by many different programs. >_>
    "Sa souvraya niende misain ye."
    - Robert Jordan, The Wheel of Time

  15. #15
    SitePoint Zealot kemp's Avatar
    Join Date
    Aug 2004
    Location
    Campinas
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks everyone.


    The JavaScript thing is not gonna happen and I'll just do it like VBulletin.


    Thanks again for the help

  16. #16
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    u cant know when someone else close the browser window without logout!
    but u can destroy the session from admin section.
    example: create an admin section with select form which take all users from db and then do that script:
    HTML Code:
    <select name="UserSession"><!-- php stuff -->
    <option value="<?php echo $row[1]; ?>"><?php echo $row[2]; ?></option><!-- end php stuff -->
    </select>
    now in php
    PHP Code:
    $data $_POST["UserSession"];
    $query mysql_query("select from user where id = ".$data);
    while(
    $row mysql_fetch_rows($query)) {
    $_SESSION["id"] = $row[1];
    }
    mysql_free_result($query); 
    put this into a form
    and when u click the delete session submit button go on this function:
    PHP Code:
    if(isset($_POST["your_submit_button"])) {
    $_SESSION["id"] = 0;

    now your selected user havent a session.
    hope this helps
    cya


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •