Jump to content

Recommended Posts

I'm trying to create a friends list type deal, and I'd like to know if there is any possible way of checking whether a user is online or not.  Each user has an active session which contains their privileges and whatnot.

Is there any way that when the user closes the window and the session eventually ends that it could perform a mysql query?

Link to comment
https://forums.phpfreaks.com/topic/83547-checking-active-users/
Share on other sites

function user_online(&$dbupdate)
{
//print_r($_SERVER);
//kijk of session_var $_SESSION['online'] al bestaat
if(!ISSET($_SESSION['online'])){
	//creeer session_var $_SESSION['online'] als sessionid $_SESSION['online'] nog niet bestaat
	$_SESSION['online']="";
	if(!isset($_SERVER['HTTP_REFERER'])){
		//echo 'no http referer';
		$_SERVER['HTTP_REFERER']='';
	}
	$Query= "INSERT INTO ppl_online (session_id, activity, ip_address, refurl, user_agent)
			VALUES ('".session_id()."', now(), '{$_SERVER['REMOTE_ADDR']}', '{$_SERVER['HTTP_REFERER']}', '{$_SERVER['HTTP_USER_AGENT']}')";
	//insert de values van de user in database
	$dbupdate->Execute($Query);
}else {
	//session_var $_SESSION['online'] bestaat al
	//kijk of session_var $_SESSION['userid'] al bestaat
	if(ISSET($_SESSION['userid'])){
		//user is ingelogd en session_var $_SESSION['online'] exists
		//update $_SESSION['online'] member = y  $_SESSION['userid']
		$dbupdate->Execute("UPDATE ppl_online SET activity=now(), member='y', UserId='".$_SESSION['userid']."' WHERE session_id='".session_id()."'");
	}else{
		//session_var $_SESSION['online'] bestaat en user is niet ingelogd
		//updaten van user zijn session_id
		$dbupdate->Execute("UPDATE ppl_online SET activity=now() WHERE session_id='".session_id()."'");
	}
}
}

 

function show_user_online(&$dbread, $long)
{
$limit_time = time() - 900; // 15 Minute time out. 60 * 15 = 900
$query_user = "SELECT * FROM ppl_online 
					WHERE UNIX_TIMESTAMP(activity) >= $limit_time 
					AND member='n' 
					GROUP BY ip_address;";
$query_member ="SELECT ppl_online.UserId, user.UserHandle FROM ppl_online, user 
					WHERE UNIX_TIMESTAMP(ppl_online.activity) >= $limit_time 
					AND ppl_online.member='y' 
					AND ppl_online.UserId = user.UserId 
					AND user.UserHide = '1' 
					GROUP BY UserId;";

$visits = count($Result01 = $dbread->Getall($query_user));
$members = count($Result02 = $dbread->Getall($query_member));
//$visits = mysql_num_rows($sql);
//$members = mysql_num_rows($sql_member);
?>
	<table class="nav" border="0" cellpadding="0" cellspacing="0">
	<tr>
	<td class="nav"  border="0" cellpadding="0" cellspacing="0">
	<?php $result=get_message(187, $dbread); display_message($result); echo count_user($dbread, $long); ?></td>
	</tr>
	<tr>
	<td class="nav"  border="0" cellpadding="0" cellspacing="0">
	<?php $result=get_message(188, $dbread); display_message($result); ?>
	</td>
	</tr>

<?php	
if($members>0)
{
?>		<tr>
	<td class="nav"  border="0" cellpadding="0" cellspacing="0">
	<?php $result=get_message(189, $dbread); display_message($result); ?><br />
	<?php 
	foreach($Result02 AS $Row)
	{
		echo ' <a href="'.$_SERVER['PHP_SELF'].'?p=p04&id='.$Row['UserId'].'">'.$Row['UserHandle'].'</a> ';
	}
?>
	</td>
	</tr>
<?php
}
?>

	<tr>
	<td class="nav" border="0" cellpadding="0" cellspacing="0">
	<?php $result=get_message(190, $dbread); display_message($result); echo $visits;?>
	</td>
	</tr>
<?php		
	//birthday();
?>
	</table>
<?php
}

 

the first function writes the info in a db.

the second function makes a table with the names of the users (linked table)

 

I think you can figure it out from here.

if you need more specific help ask me.

 

anatak

Link to comment
https://forums.phpfreaks.com/topic/83547-checking-active-users/#findComment-425088
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.