Anyone can tell me the way to display how many users are online in the site in PHP without utilizing mysql?
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
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.
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.
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?
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 ?>
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.