how many users online, PHP

Discussion in 'PHP/MySql' started by ststacytucker9, May 4, 2010.

Remove these ads by signing in
Remove these ads by signing in
  1. ststacytucker9 New Member

    Member Since:
    Feb 20, 2010
    Message Count:
    5
    Likes Received:
    0
    Anyone can tell me the way to display how many users are online in the site in PHP without utilizing mysql?
  2. Adam H Administrator

    Member Since:
    Jan 7, 2008
    Message Count:
    2,807
    Likes Received:
    162
    Occupation:
    Web Developer
    Location:
    Norfolk Uk
    Sorry im not much of a coder but from what i understand its much easier and safer with use of the database and a user table with the fields "logged in date" and "logged" . Ive found this script also which you might want to try :

    Who's Online PHP Script - Long Beach Weather
  3. ststacytucker9 New Member

    Member Since:
    Feb 20, 2010
    Message Count:
    5
    Likes Received:
    0
    Thanks for provide this script
  4. SeoKungFu Active Member

    Member Since:
    Jul 10, 2009
    Message Count:
    604
    Likes Received:
    77
    Occupation:
    SEO Ninja
    of course you can find the way of providing that info but you must have serious means why you want to avoid using database which is, in turn, the easiest and most appropriate solution.
    So if you have really sound reason for avoiding db usage, think of some file or even pipe mechanism of storing that same data, pooling it at certain ( frequent ) interval with update and read-back.
  5. jphilipson New Member

    Member Since:
    May 8, 2010
    Message Count:
    46
    Likes Received:
    1
    You need to be tracking the user sessions in some fashion or another. Typically in a database is the fastest, but it can be done with files.
  6. chococheese New Member

    Member Since:
    May 4, 2010
    Message Count:
    234
    Likes Received:
    13
    Occupation:
    Network Administrator
    Location:
    VA, USA
    Generally most people know how to do this with a database query, but it doesn't look like you want to do that. I can attempt to do a bit of research on it but what is the main reason that you want to do this without a database?
  7. balajia New Member

    Member Since:
    Jul 1, 2010
    Message Count:
    27
    Likes Received:
    1
    Occupation:
    Web Developer
    Location:
    India
    Step1: Creating the database
    create a new database called 'users', with 3 fields inside a table. These fields are timestamp, ip and file. Name the table useronline by the way. Now with the form on the PhpMyAdmin page, add the following code:

    1
    CREATE TABLE useronline (
    2
    timestamp int(15) DEFAULT '0' NOT NULL,
    3
    ip varchar(40) NOT NULL,
    4
    file varchar(100) NOT NULL,
    5
    PRIMARY KEY (timestamp),
    6
    KEY ip (ip),
    7
    KEY file (file)
    8
    );

    Step 2: The PHP Script
    view sourceprint?
    01
    <?php
    02
    //Put your basic server info here
    03
    $server = "localhost"; //normally localhost
    04
    $db_user = "username"; //your MySQL database username
    05
    $db_pass = "password"; //your MySQL database password
    06
    $database = "users";
    07
    $timeoutseconds = 300;
    08

    09
    //this is where PHP gets the time
    10
    $timestamp = time();
    11
    $timeout = $timestamp-$timeoutseconds;
    12

    13
    //connect to database
    14
    //$server = localhost probably
    15
    //$db_user = your MySQL database username
    16
    //$db_pass = //your MySQL database password
    17
    mysql_connect($server, $db_user, $db_pass);
    18

    19
    //insert the values
    20
    $insert = mysql_db_query($database, "INSERT INTO useronline VALUES
    21
    ('$timestamp','$REMOTE_ADDR','$PHP_SELF')");
    22
    if(!($insert)) {
    23
    print "Useronline Insert Failed > ";
    24
    }
    25

    26
    //delete values when they leave
    27
    $delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
    28
    if(!($delete)) {
    29
    print "Useronline Delete Failed > ";
    30
    }
    31

    32
    //grab the results
    33
    $result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'");
    34
    if(!($result)) {
    35
    print "Useronline Select Error > ";
    36
    }
    37

    38
    //number of rows = the number of people online
    39
    $user = mysql_num_rows($result);
    40

    41

    42
    //spit out the results
    43
    mysql_close();
    44
    if($user == 1) {
    45
    print("$user user online\n");
    46
    } else {
    47
    print("$user users online\n");
    48
    }
    49
    ?>
  8. PradeepKr New Member

    Member Since:
    Aug 16, 2010
    Message Count:
    22
    Likes Received:
    0
    May be you can create a text file in some folder and put the number of online users in it. Whenever somebody logsin you read the content of the file and add one to it and save it back.

Share This Page