Page 1 of 2 12 LastLast
Results 1 to 40 of 51

Thread: Counting the number of people on my site

  1. #1

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616

    Counting the number of people on my site

    I have sessions working and keeping track of any variables I need to keep track of but I am having troubles when it comes to figuring out how to track the number of people on my site and who they are.

    Now I have made it so that anytime someone views my page a session is set and they are added to a database which tracks their session id along with other various information such as their username, but what I cannot figure out is knowing when they leave my site. I just cannot figure out a decent way of going about that.

    Any ideas? Tutorials?

    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  2. #2
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    set the session to last for the time they are on the site i guess

    i havent used sessions
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  3. #3

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    Destroying the session isnt a problem is knowing that the session has been destroyed that is a problem.

    The reason that is a problem is if they do not use the logout button and just leave my site their session is destroyed but I cannot detect it unless its via JavaScript which doesnt help me at all.

    I have to find a way to either run a certain function constantly checking to see if the user is still logged on or something to that nature so I can remove them from the database.

    This is complicated
    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I haven't worked with sessions either, but is there some way you can check to see which sessions are still active each time the script is run? And update it then?

    I guess I'd have to understand more about sessions to be able to come up with a logical suggestion.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    Im not sure, my PHP book doesnt suggest a way of doing that, but you would think there would have to be one.

    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  6. #6
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    This is an interesting problem

    Since there really is no way to tell when somebody has left your site, PHP expires the sessions itself by deleting the session file after a set number of seconds. You can of course change that time in your php.ini

    It's a half-solution but probably the best there is. I've got mine set to 1 day, on the whole it works well enough. People who visited earlier that day get their existing session back, but it's not so long that it gets too inaccurate for logged-in-users count etc

  7. #7

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    well, i do not have access to the php.ini file, as I need to do this on a "virtual server" and not on my computer.

    Sooner or later i will figure it out.
    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  8. #8
    scoutt
    Guest
    well it has to be possible since this forum does it. but I think you will have to use cookies along with sessions and the database.

    now, let me through something on the table. if you use cookies along with session then when the user enters your site and logs on he sets a cookie. if you check for this cookie everytime a users comes to the site and they have one you can just set these in an array and just count the array to see how many people you have.

    I don't think you can do it with session alone since they reside on the server.

  9. #9

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    My site uses both, cookies and sessions.

    Again detecting whether the person is still on my site is complicated.

    Does anyone know how to use session_read()?? I read about it briefly but it did not have enough detail to tell me how to use it. I am wondering if I can detect whether a certain session is still open using it or if it is closed (not found).

    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  10. #10
    scoutt
    Guest
    like I said, if they have a cookie then they are logged in. like this site does on the front page. all the users down at the bottom that are logged in have a cookie. you can put all the users that have a cookie in an aray and then just count them for total users.

    and there is no such function as session_read() at least not in the manual.

  11. #11

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    but a cookie will last until you tell it to expire and what if the user isnt at your site any longer? Then he is still in the array because he didnt click the logout button.

    Are you sure there is no session_read()? I thought it was a function for reading the contents of a particular session, but maybe i misunderstood.

    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  12. #12
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    No, there is no session_read().

    http://www.php.net/manual/en/ref.session.php
    My evil laugh has a squeak in it.

    kristopherwilson.com

  13. #13

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    okay, can anyone help explain how I can manipulate this to help me? It sounds close to what I want to do.

    http://www.zend.com/zend/tut/session.php#storage

    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  14. #14
    scoutt
    Guest
    the only thing I see that you will have trouble with is that
    Note: You must set the configuration option session.save_handler to user in your php.ini file for session_set_save_handler() to take effect
    and most of the time if you don't have control over the ini file it is set to file. and this is one that you cannot change with the ini_set() function.

    I did write something that does save teh session to a database and then when the user comes back on it gets the variables form the DB and puts them back in the session. let me know if this is what you are after and I will point you in the right direction.

  15. #15

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    I have the adding them to a database part down its the removing them from it when they leave that is hard. What I was referring to in that tutorial is the home made function they use:

    read(session_id);

    if I tried doing:
    variable = read(session_id); // where the id is taken from the db and if it returns blank or false, then I can delete it right?

    Edit* Nevermind stupidity just knocked on my door. that wouldnt work

    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  16. #16

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    Is it possible to overwrite the save path and then read that directory of all the files it currently is holding?

    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  17. #17
    Lively Member
    Join Date
    Mar 2002
    Location
    England
    Posts
    79
    if you are still looking for a different way of counting the number of people on your site....

    you could.....

    create a table that stores

    IP
    TIME

    and you could add extras if you wanted like location... and when the page is loaded the table is updated with the IP address and the time the page was loaded and when ever they visit another page it checks for the IP already in the table and updates it if its there, if not creates a new record... also dont forget to delete the records... you can set a time like 5mins and each time the table is updated when each page is loaded all records that hold a time that is more that 5 mins ago are deleted as being no active users...

    hope that makes sence... i will check back later

    mjlogan

  18. #18

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    hmm, keeping track of the time and deleting them if they are not within a certain time range. sounds easy enough, but then again might not be.

    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  19. #19
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    BTW, nice site, Matt.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  20. #20
    Lively Member
    Join Date
    Mar 2002
    Location
    England
    Posts
    79
    i could post the code if you want?? iam sure ive got it somewhere

  21. #21

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    ya, if you dont mind I would like to see the code.

    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  22. #22
    Lively Member
    Join Date
    Mar 2002
    Location
    England
    Posts
    79
    k i think this is all you need.. hope it helps

    PHP Code:
    $time time();
    $timeout $time-300;
    $query mysql_query("DELETE FROM online WHERE $time < $timeout"); 
    $query mysql_query("INSERT INTO online VALUES ('', '$time')"); 
    mjlogan

  23. #23

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    That looks exactly what I have been attempting to do. I will give it a shot when I get home.

    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  24. #24
    Lively Member
    Join Date
    Mar 2002
    Location
    England
    Posts
    79
    ok... glad to be some help...

    let me know who it goes

  25. #25
    scoutt
    Guest
    Originally posted by mjlogan
    k i think this is all you need.. hope it helps

    PHP Code:
    $time time();
    $timeout $time-300;
    $query mysql_query("DELETE FROM online WHERE $time < $timeout"); 
    $query mysql_query("INSERT INTO online VALUES ('', '$time')"); 
    mjlogan
    that won't work. what i fI was at your site just sitting idle for 15 minutes and then when I went and refreshed it logged me out (deleted me from the DB) I would have to log in again and that would get annoying.

    Matt you say you have no problems with storing the session? are you storing the session or the varialbes?
    look this site does it with cookies and sessions and knows who's online and who's not. as soon as I shut my browser down it notices it and takes my name off the front page. and how I expained it before was almost how it worked. let me look into it farther so I can give you a clear definition of how it works.

  26. #26

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    No the above works, because my login will not delete your session but rather just delete you from the database if you are idle for more than 5 minutes. That in no way has direct effect on your sessions or cookies I have placed.

    I am testing this right now and its working so far.

    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  27. #27
    scoutt
    Guest
    ok, that makes sense

  28. #28

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    Actually, you were partially right that it wouldnt work so I modified the parts that were a bit off and came up with this:

    PHP Code:
        //Define the session id so I can track each person and update their info accordingly.
        
    $sessionNumber session_id();
        if (
    $tbUser == ""$tbUser "Guest";
        
    $timeSession time(); 

        
    //Check to see who has surpased the in-active time limit of 5 minutes (i had to change this part as mjlogan's didnt work)
        
    $querySession "select * from tbOnline";
        
    $resultSession mysql_query($querySession);
        
    $numSessions mysql_numrows($resultSession);
        
    $countSession 0;

        while (
    $countSession $numSessions) {
            
    $idSession mysql_result($resultSession,$countSession,"id");
            
    $sessionStart mysql_result($resultSession,$countSession,"time");

            if (
    $sessionStart $timeSession)
                
    $query mysql_query("delete from tbOnline where id='$idSession'"); 

            
    $countSession++;
        }

        
    //See if the user's session id is in the database and if it is update it rather than adding a duplicate row
        
    $foundUser mysql_numrows(mysql_query("select * from tbOnline where session='$sessionNumber'"));
        if (
    $foundUser == 0)
            
    $query mysql_query("insert into tbOnline (username,time,session) values ('$tbUser', '$timeSession','$sessionNumber')");
        else
            
    $query mysql_query("update tbOnline set username='$tbUser',time = '$timeSession' where session='$sessionNumber'"); 
    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  29. #29
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Where can I find some snappin' tutorials/examples of sessions? I've looked through the functions, but I'm lost?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  30. #30

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    I read about them in my php 4 bible book, but the only thing you have to know really is:

    PHP Code:
    session_start();  // self-explanatory i think
    session_register('variableName'); // register a variable for session use
    session_unregister('variableName'); // destroy a session variable
    session_destroy(); // destroys all session variables and the session id
    session_id(); // grabs the user's session id so you can pass it, or save it in a database 
    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  31. #31
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    A visitor accessing your web site is assigned an unique id, the so-called session id. This is either stored in a cookie on the user side or is propagated in the URL.

    The session support allows you to register arbitrary numbers of variables to be preserved across requests. When a visitor accesses your site, PHP will check automatically (if session.auto_start is set to 1) or on your request (explicitly through session_start() or implicitly through session_register()) whether a specific session id has been sent with the request. If this is the case, the prior saved environment is recreated.

    All registered variables are serialized after the request finishes. Registered variables which are undefined are marked as being not defined. On subsequent accesses, these are not defined by the session module unless the user defines them later.
    Those three paragraphs just gave me a big headache...
    My evil laugh has a squeak in it.

    kristopherwilson.com

  32. #32
    scoutt
    Guest
    ***clears throat***

    my site has some tutorials (links) to sessions.

    and by the way Matt, those session functions you mention are deprecated and you should be using $_SESSIONS[] instead.

  33. #33
    scoutt
    Guest
    hobo jsut think of them as a cookie as they function in the same manner, it just that they are on the server.

  34. #34

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    Originally posted by scoutt
    ***clears throat***

    my site has some tutorials (links) to sessions.

    and by the way Matt, those session functions you mention are deprecated and you should be using $_SESSIONS[] instead.
    Can you tell me which ones? Is session_start() still valid? and session_destroy()?

    I am assuming its just $_SESSIONS['variable'] for registering the variable instead of session_register('variable') but how would you destroy/unregister a variable?

    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  35. #35
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    PHP Code:
    unset($_SESSION['var']); 
    My evil laugh has a squeak in it.

    kristopherwilson.com

  36. #36
    scoutt
    Guest
    session start is still in use, to register a varialbe to a session it is simple.

    $_SESSION['name'] = $variable

    and to destroy it it is

    session_start();
    unset($_SESSION['name']);
    session_unset();

    I don't think session_unset() is needed but I add it for catuionary sake.

    the manul has all the info you need to create and write to a session using the superglobal. but that is pretty much it.

  37. #37

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    Okay does this follow the new standards or is there anything I should fix?

    PHP Code:
        session_start();
        
    $_SESSION["tbUser"];
        
    $_SESSION["tbPass"];

        
    $sessionNumber session_id();
        if (
    $tbUser == ""$tbUser "Guest";
        
    $timeSession time(); 

        
    $querySession "select * from tbOnline";
        
    $resultSession mysql_query($querySession);
        
    $numSessions mysql_numrows($resultSession);
        
    $countSession 0;

        while (
    $countSession $numSessions) {
            
    $idSession mysql_result($resultSession,$countSession,"id");
            
    $sessionStart mysql_result($resultSession,$countSession,"time");

            if (
    $sessionStart $timeSession)
                
    $query mysql_query("delete from tbOnline where id='$idSession'"); 

            
    $countSession++;
        }

        
    $foundUser mysql_numrows(mysql_query("select * from tbOnline where session='$sessionNumber'"));
        if (
    $foundUser == 0)
            
    $query mysql_query("insert into tbOnline (username,time,session) values ('$tbUser', '$timeSession','$sessionNumber')");
        else
            
    $query mysql_query("update tbOnline set username='$tbUser',time = '$timeSession' where session='$sessionNumber'"); 
    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  38. #38
    scoutt
    Guest
    looks ok. look at my changes. the session variables should be used just like any ofther variable.
    PHP Code:

    session_start
    ();    

    $_SESSION["tbPass"]; // this is doing nothing sitting here.

        
    $sessionNumber session_id(); // not sure if you can use this on the super global. let me know, seems like I tried and couldn't get anything
        
    if ($_SESSION["tbUser"] == ""$tbUser "Guest";
        
    $timeSession time(); 

        
    $querySession "select * from tbOnline";
        
    $resultSession mysql_query($querySession);
        
    $numSessions mysql_numrows($resultSession);
        
    $countSession 0;

        while (
    $countSession $numSessions) {
            
    $idSession mysql_result($resultSession,$countSession,"id");
            
    $sessionStart mysql_result($resultSession,$countSession,"time");

            if (
    $sessionStart $timeSession)
                
    $query mysql_query("delete from tbOnline where id='$idSession'"); 

            
    $countSession++;
        }

        
    $foundUser mysql_numrows(mysql_query("select * from tbOnline where session='$sessionNumber'"));
        if (
    $foundUser == 0)
            
    $query mysql_query("insert into tbOnline (username,time,session) values ('$tbUser', '$timeSession','$sessionNumber')");
        else
            
    $query mysql_query("update tbOnline set username='".$_SESSION["tbUser"]."',time = '$timeSession' where session='$sessionNumber'"); 

  39. #39

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    The only things you changed are what I need to keep. The $tbUser and $tbPass are also cookies and so I have those two variables constantly passing information to sessions named the same thing, plus they are also created when the login form is filled out, so they are very important.

    session_id() still works for me and in the manual it says its good.

    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  40. #40
    scoutt
    Guest
    so basically all you need the sessin for is the session_id(), hmmmm I don't see any sense in that. what is the id doing for you?

Page 1 of 2 12 LastLast

Posting Permissions

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



Click Here to Expand Forum to Full Width