2014-10-11 64 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