KillerDucky1
KillerDucky1

Reputation: 17

How to check is someone is online?

So i have made a simple log in system in php how would i go on making a function on php to check if someone is online also can it be done in php

login system

<?php
if (empty($_POST) === false) {
$username = $_POST['username'];
$password = $_POST['password'];

if (empty($username) === true || empty($password) === true) {
   $errors[] = 'Please Enter a username and password';
   } else if (user_exists($username) === false) {
   $errors[] ='Sorry but the user you entered dose not exist';
  } else if (user_active($username) === false) {
   $errors[] ='You have not activated your account please check your email to activate your account';
  } else {
if (strlen($password) > 32){
$errors[] ='Passwords to long !';
}
 $login = login($username, $password);
 if ($login === false) {
 $errors[] ='incorrect username or password';
 } else {
$_SESSION['user_id'] = $login;
 header('Location: index.php');
 exit();
       }
    }
  } else {
  $errors[] = 'No information received';
}
echo output_errors($errors);
?>

Upvotes: 0

Views: 159

Answers (1)

Ido
Ido

Reputation: 2054

You can use sessions for this:

http://php.net/manual/en/book.session.php

Simple usage of sessions:

// Start the session
if(!session_id())
    session_start();

$_SESSION["the_user"] = true;

// To check if the session is alive
if(!empty($_SESSION["the_user"]))
    echo "User is online !";
else
    echo "User isn't online!";

// Delete the session
unset($_SESSION["the_user"]);

Note that this is just a simple usage of the session, the session will be alive even if the user went of the site. but it will be for a few minutes. (session's expire time)

Upvotes: 2

Related Questions