2014-10-11 63 views
0

,所以我必須在php如何將我去製作上php一個函數來檢查,如果有人也在網上可以將其在php如何檢查是否有人在線?

登錄系統

<?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); 
?> 
+1

檢查該用戶的會話是否存在。 – 2014-10-11 17:00:07

+0

當任何用戶登錄設置標誌在數據庫中激活,然後進行數據庫調用來獲取所有在數據庫中有活動標誌的用戶。 – 2014-10-11 17:05:53

+0

@Azam alvi謝謝你的迴應,但我試過了,它似乎比我的回答更復雜 – KillerDucky1 2014-10-11 17:13:01

回答

2

你做了一個簡單的日誌系統可以使用會話此:

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

會議的簡單用法:

// 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"]); 

請注意,這只是會話的一個簡單用法,即使用戶訪問該網站,會話仍然存在。但會持續幾分鐘。 (會話的到期時間)

+0

它只顯示該人只在那裏在線屏幕 – KillerDucky1 2014-10-13 05:09:15

+0

使用redis或類似的庫來保存服務器上的實時會話。那麼您可以輕鬆知道當前有多少用戶在線。 – Ido 2014-10-13 09:34:16