Online Users and Guests

Written by @m_k_amin 25 December 2012

Some websites show to the visitors who is online now. There also exist a distinction between users and guests. The aim of this script is to calculate the number of guests and show online users. It also has intelligence to the users and guests existence. It has a Login form in it, but note that this script doesn't contain Login-Registration system because of avoiding redundant; all the users are accepted here. If you need Login-Registration system see http://www.phpfreecode.com/Login_-_Register_System.htm This script uses both cookies and file functions and just is a one page script. It makes what it needs. Enjoy!

Code Snippet:

                                                
                                                <!-- this script is provided by www.phpfreecode.com coded by: Kerixa Inc. -->
<!-- This Script is from www.phpfreecpde.com, Coded by: Kerixa Inc-->
<?php
if (!is_dir('OU')) mkdir('OU');
if (!is_dir('OG')) mkdir('OG');
if (isset($_POST['logout'])) setcookie('user',$_GET['user'],time()-1000);
$files = scandir('OU');
foreach ($files as $user){
	if ($user=='.' || $user=='..') continue;
	$handle=fopen("OU/$user",'r');
	$time = fread($handle, filesize("OU/$user"));
	fclose($handle);
	if ((time()-$time)>6) unlink("OU/$user");
}
$gcount=0;
$files = scandir('OG');
foreach ($files as $user){
	if ($user=='.' || $user=='..') continue;
	$handle=fopen("OG/$user",'r');
	$time = fread($handle, filesize("OG/$user"));
	fclose($handle);
	if ((time()-$time)>6) {unlink("OG/$user");$gcount--;}
	$gcount++;
}

if (!is_file('check.php')){
	$handle = fopen("check.php", "w");
	$c='<meta http-equiv="refresh" content="3">
<?php
if (isset($_GET["user"]) && isset($_GET["type"])){
	$user=$_GET["user"];
	$type=$_GET["type"];
	if ($type=="user"){
		$handle = fopen("OU/$user", "w");
		fwrite($handle, time());
		fclose($handle);
	}else{
		$handle = fopen("OG/$user", "w");
		fwrite($handle, time());
		fclose($handle);
	}
}
?>';
	fwrite($handle, $c);
	fclose($handle);
}

if (isset($_POST['submit'])){
if ($_POST['user']!=''){
	setcookie('user',$user,time()+1000);
	$user=$_POST['user'];
	$handle = fopen("OU/$user", "w");
	fwrite($handle, time());
	fclose($handle);
		$type='user';
}}else{
	if (isset($_COOKIE['me']))
		$user=$_COOKIE['me'];
	else{
		$gcount++;
		$user='Guest'.(rand(1,999)*rand(1,999)/rand(1,999));
		setcookie('me',$user);}
	$handle = fopen("OG/$user", "w");
	fwrite($handle, time());
	fclose($handle);
	$type='guest';
}

$files = scandir('OU');
$users='';
foreach ($files as $userd) if ($userd!='.' && $userd!='..') $users.='and '.$userd."<br>";
?>
<table style="border: 1px solid #000000; width: 343px; background-color: #F8F8D7;" align="center">
	<?php if (!isset($_POST['submit'])&& !isset($_COOKIE['user'])){ ?>
	<form action="" method="post">
	<tr>
		<td style="padding:5px;width: 105px; font-family: 'Times New Roman', Times, serif; font-size: 14pt; border-left-style: solid; border-left-width: 0; border-top-style: solid; border-top-width: 0; border-right-width: 0; border-bottom-width: 0;">User Name:</td>
		<td style="padding:5px;border-right-style: solid; border-right-width: 0; border-top-style: solid; border-top-width: 0; width: 123px; border-left-width: 0; border-bottom-width: 0;">
		<input name="user" style="width: 220px; font-size: 14pt; font-family: 'Times New Roman', Times, serif;"></td>
	</tr>
	<tr>
		<td style="padding:5px;width: 105px; font-family: 'Times New Roman', Times, serif; font-size: 14pt; border-left-style: solid; border-left-width: 0; border-right-width: 0; border-top-width: 0; border-bottom-width: 0;">Password:</td>
		<td style="padding:5px;border-right-style: solid; border-right-width: 0; border-bottom-style: solid; border-bottom-width: 0; width: 123px; border-left-width: 0; border-top-width: 0;">
		<input name="pass" style="width: 170px;width: 220px; font-size: 14pt;" type="password"></td>
	</tr>
	<tr>
		<td style="padding:5px; font-family: 'Times New Roman', Times, serif; font-size: 14pt; text-align: right; border-left-style: solid; border-left-width: 0; border-right-style: solid; border-right-width: 0; border-bottom-style: solid; border-bottom-width: 0; border-top-width: 0;" colspan="2">
		<br>
		<input name="submit" style="font-size: 14pt; font-family: 'times New Roman', Times, serif; color: #F132A8; width: 80px;" type="submit" value="Login"></td>
	</tr>
	</form>
	<?php }else { ?>
		<form action="<?php echo $_SERVER['PHP_SELF'].'?user=$user' ?>" method="post">
		<td style="padding:5px; font-family: 'Times New Roman', Times, serif; font-size: 14pt; text-align: right; border-left-style: solid; border-left-width: 0; border-right-style: solid; border-right-width: 0; border-bottom-style: solid; border-bottom-width: 0; border-top-width: 0;" colspan="2">
		<br>
		<input name="logout" style="font-size: 14pt; font-family: 'times New Roman', Times, serif; color: #F132A8; width: 80px;" type="submit" value="Logout"></td>	
		</form>
	<?php } ?>
</table>
<table style="border: 1px solid #000000; width: 343px; background-color: #D7F6F8;" align="center">
	<tr>
		<td style="padding:5px;font-family: 'Times New Roman', Times, serif; font-size: 14pt; border-left-style: solid; border-left-width: 0; border-top-style: solid; border-top-width: 0; text-align: center; border-right-width: 0; border-bottom-width: 0;">
		Who is Online?</td>
	</tr>
	<tr>
		<td style="padding:5px;font-family: 'Times New Roman', Times, serif; font-size: 14pt; border-left-style: solid; border-left-width: 0; text-align: center; border-right-width: 0; border-top-width: 0; border-bottom-width: 0;">
<span style="color: #7B0909">
<?php
echo "$gcount Guests<br>";
echo($users);
?>
	</span>
	</tr>
</table>
<div style="text-align: center"><br>
<font face="Tahoma"><a target="_blank" href="http://www.phpfreecode.com/">
<span style="font-size: 8pt; text-decoration: none">PHP Free Code</span></a></font>
</div>

<iframe id="I1" border="1" frameborder="1" name="I1" style="visibility:hidden;display:none" src="<?php echo "check.php?user=$user&type=$type" ?>" >Your browser does not support inline frames or is currently configured not to display inline frames.
			</iframe>
<a target='_blank' href='www.phpfreecode.com' style='font-size: 8pt; text-decoration: none'>Php Best Codes</a>                                                
                                            

Example:


About @m_k_amin

This user is pending a biography.

M

Comments


Here you can leave us commments. Let us know what you think about this code tutorial!

0 / 300

TRENDING POST
1
2
3
4
5
VISITORS
Online Users: 12
Recent Members: grkkid, Manaakividuinfo.com, karticksv, sava, tinatina
advertisement 2