SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2000
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    sessions online users

    hello again

    my next problem is as follows...

    i have a signin site and in the page accessible only to members there is a section updating all the time with a refresh with a list of users currently online. Fine all this works and I have done the signout part no problem which deletes the users entry in the sessions table and kills the cookie on his machine at the same time.

    now if the person does not logout i have the cookie set to expire in an hour, so for that user when they come back they will have to log on, however the entry in the sessions table will stay there so to other users on the site it will look like the person is always logged on, get me??

    so is there a way to kill the entry in the sessions table after a certain amount of time or will using sessions in php4 help solve this problem in any way.

    i'd be gratefull for any advice anyone can give me.

    Garrett

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Sessions expire after a short time by themselves. I think that "session.gc_maxlifetime" in php.ini controls how long that is.

    I am not really sure how sessions fit into your scheme. It sounds like when someone logs in, you just put their name in a table called something like currently_online, and then when they log out you take their name out of the table.
    Last edited by 7stud; Jun 2, 2001 at 21:27.

  3. #3
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    yes

    Here would be my suggestion:

    Have your online users page say something like this:

    ----------------------------------------
    Users active in the past ten minutes:
    foo
    bar
    blah
    exbabylon
    Garrett_44
    7_stud
    ----------------------------------------

    Now, I would make another column in your table of online users called time or date. In that, store the time that they logged in, and then you can go access all of the entries that are newer than ten minutes.

    I would take it a step further. At the top of every page, mabey even use an include, have some script like this:

    PHP Code:
    $sql "UPDATE online_users SET time='DATETIME()', activity='$REQUEST_URI' WHERE sessionid='$PHPSESSIONID'";
    if(!
    mysql_query($sql)){
    echo(
    "Database Error");
    exit();

    Hope that helps,

    God Bless,

    Alex
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2000
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    heres what i am doing, when someone logs on an entry is created in a table called "sessions" with the users id, ipaddress, and time they logged on.

    on the index page of the site (the page all users arrive at first), the page checks if any of the sessions in the table "sessions" are out of date, this i want to change to "DELETE FROM"

    $query = "SELECT FROM sessions WHERE (created < time()-1200)";
    $result = mysql_query($query);

    however not sure how if the table is empty i can cause something to happen, for example for the moment i am using the following and when the table is empty i get an error 'Supplied argument is not a valid MySQL result resource'

    if(mysql_num_rows($result))
    {
    echo "entries";
    }
    else
    {
    echo "no entries";
    }

    so my question is how do i test if the table is..
    1 - empty
    2 - has sessions out of date
    3 - has sessions still active


    I am also wondering how does the script which deals with deleting sessions out of date act if the session is out of date but the user is still on the site? does the session get deleted and the next time that user goes to a new page in the site get logged off automatically and redirected to the log in page?

    hope my text is clear and you can understand what i am trying to ask. thanks in advance!

    Garrett

  5. #5
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No offence, but you're making this way more complacated than it has to be.

    You could just create a table, a session will probably never be the same thing, so don't worry about deleting them. Just draw out of the table where time > time()-600 (ten minutes)

    Now, here is the really tricky part, let me create a few cenareos for you, and correct me if I'm wrong.

    Canareo One:
    I go to your site, login, then surf for an hour, after like 20 minutes ir whatever, I'm no longer in your active users list, even though I'm actively surfing your site, because I logged in like two hours ago, I'm not listed as "online" anymore, even though I actually am.

    Cenaroe Two:
    I go to your site, open all the new news about your brand new beta release of this spiffy program. I continue to surf the internet, leaving that page open in it's seperate window. I'm still on your site, but after two hours I'm no longer on your logged in user list.

    Now, the second cenaroe is not quite such a bad thing, but how about the first? You must devise a way to "track" a visitor's last move on your site, that way you will know if they are still acitve or not. Perhaps have an "active" users list, instead of an online users list.

    Don't worry about deleting anything from the table. No need to, just use the time as your yard stick. Here is an example of what I would do in your situation, and see if it may help you:

    Add another column to your sessions table call it "sessionid" This one step will allow you to NOT have to delete anything from this table.

    Now, when a user logs in:

    PHP Code:
    <?php
    // Login.php
    if($action == "signup){
    // create sessions and variables

    $sql = "INSERT INTO sessions SET sessionid='$PHPSESSID'ip='$REMOTE_ADDR'userid='$userid, time='UNIX_TIMESTAMP'();
    if(!mysql_query($sql)){
    echo("Database Error");
    } else {
    echo("Thank you for logging in $username");

    } else {
    // Display Form
    }
    ?>
    And in make an include file:
    PHP Code:
    <?php
    if(!mysql_num_rows(mysql_query(SELECT sessionid FROM sessions WHERE sessionid='$PHPSESSID'))){
        
    header(Locationlogin.php);
        exit();
    } else {
        if(!
    mysql_query(UPDATE sessions SET time='UNIX_TIMESTAMP()' WHERE sessionid='$PHPSESSID')){
            echo(
    "Database Error");
            exit();
        }
    ?>
    Include this file in all of your pages. This will update the time to the user's last activity.

    Now for your online user's page:

    PHP Code:
    include("update.inc");

    $sql "SELECT * FROM sessions WHERE time > (UNIX_TIMESTAMP() - 600)";
    $result mysql_query($sql);
    if(!
    $result){
    echo(
    "Database Error");
    } else {
    $i mysql_num_rows($result);
        if(!
    $result){
            echo(
    "No online users");
        } else {
           while(
    $row mysql_fetch_array($result)){
                
    extract($row);
                echo(
    "$userid$ip$time");
            }
        }

    God Bless,

    Alex Stanton
    Last edited by exbabylon; Jun 4, 2001 at 05:51.
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services


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
  •