How to make "online user" script with php

in #utopian-io7 years ago

PHP is well known server side language for web programming. Solutions of web needs, php is the language you can find resources on the web easily. Here in this contribution i will explain how to make "online user" table for your website.

Hey dear utopians, this time i would love to show something at least might be usefull for your web projects via php.

Why do we need to collect the data that how many users online in our system ?

Like the most of the projects, statistics the key of understanding capacity of your project. How many users online, how many posts included our system etc.

I consider you are already created your register and log-in system,

Something like this;

We have already registered user, we need to find who is that user by session().

In the first line of the php tag we wrote session_start();. That means, server giving spesific id to the user who connected to our system. like this;

Connect to your database, mine is under the system folder "dbconn.php" file. So i have to create a connection to my database to insert data of user and session of user.

Lets put our terms to get data only from logged in users.

if (!isset($_SESSION['user'])!="" ) {
header("Location: login.php");
exit;
}

If there isnt session(that means unregistered users), user will redirect to the "login.php".

We need to get the data of registered user;
$querywho=mysqli_query($conn, "SELECT * FROM users WHERE userID=".$_SESSION['user']);
$userRow=mysqli_fetch_array($querywho);
$thatuser = $userRow['userName'];

From the database of mine, ive selected all userID's and asked who is that person ? which id ? and then ive created new variable to collect those userName's as an array.

We found out who has connected our system and what is his username right now!
Lets make a step forward to create our extra table only for recording sessions.

We seperated the table from the users, we will update this table based on time, the key of web programming is flexible coding. You have to seperate everything as much as you can.

We may turn back to coding now our online users system

ive created variable which named session and get id of user.
$session = session_id();
and created new variable which names sessname to get username of logged in user via session,
$sessname = $userRow['userName'];
Coding based on time, every 5 minute database will be updated with new sessions, or remove who has disappear.
$timetime = time();
$timecheck = $timetime-300;
select the session,
$sqlsess="SELECT * FROM sess WHERE session='$session'";
create query,
$result=mysqli_query($conn, $sqlsess);
count time,
$counttaime=mysqli_num_rows($result);

Now all our variables, connections and data we got are done,

Here we go;

if($counttaime == 0){
$sqlsesso="INSERT INTO sess (session, sestime, useruser) VALUES ('$session', '$timetime', '$sessname')";
$result1=mysqli_query($conn, $sqlsesso);

else {
$sql2 = "UPDATE sess SET sestime='$timetime' WHERE session=".'$session';
$result2=mysqli_query($conn, $sql2);
}

So basicly we said if counttime is equal to 0, insert the data of user with sessionid into our table.

$sql3="SELECT useruser FROM sess WHERE 1";
$result3=mysqli_query($conn, $sql3);
$count_user_online=mysqli_num_rows($result3);

to the $count_user_online variable we put number of rows online we have.

If over 10 minute, delete session from the table,

$sql4="DELETE FROM sess WHERE sestime<$timecheck";
$result4=mysqli_query($conn,$sql4);

So how we will gonna show or see by our own who is literally online ? Currently we just coded the backscene, time for a show it up!

Loop we should create,

Now i have my online user list in the table;

Lets make test with 2 different browser and 2 users.

I hope this may help you to make your project better.

Regards

Header logo source : LOGO



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Holy crap thank you. I'm learning PHP in uni at the moment and it's cool to see a post like this.

Enjoy my friend :)

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Good job my friend..

Hey @omeratagun I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.19
TRX 0.17
JST 0.030
BTC 80568.31
ETH 3220.00
USDT 1.00
SBD 2.80