"Who's online?" always = 0

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Breana
    New Member
    • Aug 2007
    • 117

    "Who's online?" always = 0

    Ok i found this a while back and i am trying to mod it so it works but it keeps not updating the users online and kills my other code?

    The first step in creating your "who's online" counter is to setup a table in your database. Here's the table that I used:

    Code:[code=mysql]
    CREATE TABLE trv_online (
    session_id varchar(255) NOT NULL default '',
    activity datetime NOT NULL default '0000-00-00 00:00:00',
    member enum('y','n') default 'n',
    ip_address varchar(255) NOT NULL default '',
    refurl varchar(255) NOT NULL default '',
    user_agent varchar(255) default NULL,
    PRIMARY KEY (session_id),
    KEY session_id (session_id)
    ) TYPE=MyISAM;[/code]


    The first script we'll dig into is going to be included as a file at the top of every php script on your website. Hopefully you have a global header file that you can simply do this only one time. Here's the script:

    Code:[code=php]
    if(!session_is_ registered('onl ine')){
    @mysql_query("I NSERT INTO trv_online (session_id, activity, ip_address, refurl, user_agent)
    VALUES
    ('".session_id( )."', now(), '{$_SERVER['REMOTE_ADDR']}', '{$_SERVER['HTTP_REFERER']}', '{$_SERVER['HTTP_USER_AGEN T']}')");
    session_registe r('online');
    } else {
    if(session_is_r egistered('user _id')){
    @mysql_query("U PDATE trv_online SET activity=now(), member='y' WHERE session_id='".s ession_id()."'" );
    }
    }[/code]

    Place in body:
    Code:[code=php]
    <?
    // This file is included into your website
    // Preferably a MySQL connection has been established already

    $limit_time = time() - 300; // 5 Minute time out. 60 * 5 = 300
    $sql = mysql_query("SE LECT * FROM trv_online WHERE UNIX_TIMESTAMP( activity) >= $limit_time AND member='n' GROUP BY ip_address") or die (mysql_error()) ;
    $sql_member = mysql_query("SE LECT * FROM trv_online WHERE UNIX_TIMESTAMP( activity) >= $limit_time AND member='y' GROUP BY ip_address") or die (mysql_error()) ;
    $visits = mysql_num_rows( $sql);
    $members = mysql_num_rows( $sql_member);

    echo "People Online:<br />";
    echo "Guests Online: $visits<br />";
    echo "Members Online: $members<br />";
    ?>[/code]
    Well i got it working but it dont update when i login?
    It says 14 guest now, and i am logged in so it sould ream me as member.....

    And how do i make a cron job, the tutorial says it lowrs site badwith and it runs better updating records?

    [code=php]if(session_is_r egistered('onli ne')){
    @mysql_query("U PDATE trv_online SET activity=now() WHERE session_id='".s ession_id()."'" );
    }
    // Database connection information here

    $maxtime = time() -100;
    $sql = mysql_query("DE LETE FROM trv_online WHERE UNIX_TIMESTAMP( activity) < '$maxtime'");
    $rows = mysql_affected_ rows();[/code]
    Last edited by pbmods; Aug 23 '07, 11:56 PM. Reason: Added [CODE] tags.
  • Breana
    New Member
    • Aug 2007
    • 117

    #2
    Any got an idea on this?

    Comment

    • Breana
      New Member
      • Aug 2007
      • 117

      #3
      Bump, i need help :)

      Comment

      • etiainen
        New Member
        • Aug 2007
        • 40

        #4
        does it register you as a member after a few clicks?
        What I mean is, does it only break when you login or everytime it executes?

        Comment

        • Breana
          New Member
          • Aug 2007
          • 117

          #5
          Wel it updates the guest because it keeps going up and down when i login on the laptop and my pc but if i login it wont show...
          always Members online: 0

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Heya, Breana.

            Changed thread title to better describe the problem.

            You're getting a '0' response because your query generates no results. This probably means that the `member` field never gets set to 'y'.

            Comment

            • etiainen
              New Member
              • Aug 2007
              • 40

              #7
              Hm, this could be it:

              [PHP]
              if(!session_is_ registered('onl ine')){
              @mysql_query("I NSERT INTO trv_online (session_id, activity, ip_address, refurl, user_agent)
              VALUES
              ('".session_id( )."', now(), '{$_SERVER['REMOTE_ADDR']}', '{$_SERVER['HTTP_REFERER']}', '{$_SERVER['HTTP_USER_AGEN T']}')");
              session_registe r('online');
              } else {
              if(session_is_r egistered('user _id')){
              @mysql_query("U PDATE trv_online SET activity=now(), member='y' WHERE session_id='".s ession_id()."'" );
              }
              }
              [/PHP]

              In this snippet of yours you have this structure:
              if session not registered
              .......register it
              else
              .......if user_id registered in session
              ..............m ark it as an online member (set that thing='y')


              Take a look at this... when you login first time you have no session and it registers it, but in that same pass it does not mark you as an online member 'cause it's in the else branch...


              Another problem might be that your session auto start isn't set to true, so you must call session_start() in your script before referencing $_SESSION...
              Last edited by etiainen; Aug 26 '07, 10:33 AM. Reason: added dots for indentation

              Comment

              • Breana
                New Member
                • Aug 2007
                • 117

                #8
                Ok, so i changed it to this: and placed it in the very top where i have it refrence session start...

                [PHP] session_start() ;
                include("./config.php");
                include("./dbx.php");
                include("./common.php");

                if(session_is_r egistered('user _id')){
                @mysql_query("U PDATE trv_online SET activity=now(), member='y' WHERE session_id='".s ession_id()."'" );
                } else {
                if(!session_is_ registered('onl ine')){
                @mysql_query("I NSERT INTO trv_online (session_id, activity, ip_address, refurl, user_agent)
                VALUES
                ('".session_id( )."', now(), '{$_SERVER['REMOTE_ADDR']}', '{$_SERVER['HTTP_REFERER']}', '{$_SERVER['HTTP_USER_AGEN T']}')");
                session_registe r('online');
                }
                }
                if(session_is_r egistered('onli ne')){
                @mysql_query("U PDATE trv_online SET activity=now() WHERE session_id='".s ession_id()."'" );
                }[/PHP]

                I just need a simply way for it to update if a member logs in.
                Last edited by Breana; Aug 26 '07, 01:35 PM. Reason: opps in text :)

                Comment

                • etiainen
                  New Member
                  • Aug 2007
                  • 40

                  #9
                  Ok, this structure looks fine to me...
                  Is it working?

                  Comment

                  • Breana
                    New Member
                    • Aug 2007
                    • 117

                    #10
                    Hey yeah i got it working, i changed 2 lines and put the code in a new php and it does work thanks for the help.

                    I had some code in the head killing it lol i dident know exit; ment it would not load past that i thought it ment exit thet function then load a new code :P

                    I know now lol.

                    Comment

                    • pbmods
                      Recognized Expert Expert
                      • Apr 2007
                      • 5821

                      #11
                      Heya, Breana.

                      Glad to hear you got it working! Good luck with your project, and as always, if you ever need anything, post back anytime :)

                      Comment

                      Working...