OxyScripts.com
Menu spacer Home Tutorials Articles Code Forums irc.freenode.net #oxyscripts
Main (PHP)
Home Forums PHP News PHP Tutorials Articles PHP Code Snippets Contact Us Sysadmin Resources Books Template Shop
3rd Party Streams
SlashDot PHPDeveloper.org PHP.Net
Resources
PHP Manual MySQL Manual Smarty Manual PEAR Manual PHP-GTK Manual Symfony Manual
Code Snippets
Authentication Database Graphics HTTP Miscellaneous Time/Date
Affiliates
Scripts TutorialMan TutorialGuide CodingForums.com PHP Scripts Cheap Web Hosting Affordable Web Hosting

Search This Site :     PHP Function Reference :
 

Users Online

By Chad (2003-07-03. 18755 views.)
Display number of people currently viewing your site.

First, i will assume that you have a MySQL database, and that you know how to use it.
The very first step you must take is to dump the following data into your database

<?php

CREATE TABLE joined 

ip char(15NOT NULL
hl timestamp(14), 
PRIMARY KEY (ip
);
 
?>




The next step is to go to the PHP document where you wish to display the number of people on your site and add the following code ( it looks like a lot, but it's really not. I just like to comment and keep things spaced out to make things easy )
This code can be placed anywhere on the page (i suggest near the top)


<?php
 
<?php 
 
// database information
$dbhost="host address";
$dbuser="user name";
$dbpass="password";
$dbname="database name";
 
// disconnected timeout
$timeout 60;  // in seconds.. you can make it higher or lower.. i suggest keeping it at 1 minute
 
// this will tell apart the people on your site 
$ip=getenv("REMOTE_ADDR");
 
//connect to database
mysql_connect($dbhost,$dbuser,$dbpass); 
 
// remove old connections so that the number doesn't continue to rise like a counter
$strq "DELETE FROM joined WHERE hl < now()"
$qry mysql_db_query($dbname,$strq); 
$strq "SELECT count(*) FROM joined WHERE IP='$ip'"
$qry mysql_db_query($$dbname,$strq); 
 
while ( 
$r = @mysql_fetch_array($qry) ) { 
    
$ppl $r["count(*)"];

 
if (
$cpt) { 
// checks for reloads, etc..
    
$strq "UPDATE joined SET hl=hl + $timeout WHERE ip='$ip'";
} else { 
// confirmed user updated into database
    
$strq "INSERT INTO joined (ip, hl) VALUES ('$ip', now()+ $timeout)";

$qry mysql_db_query($dbname,$strq); 
 
// display the number of connected people 
$strq "SELECT count(*) FROM joined"
$qry mysql_db_query($dbname$strq);
 
while ( 
$result = @mysql_fetch_array($qry) ) { 
    
$ppl $result["count(*)"];

?>
 
?>


Now for the last, and easiest step. Simply add the following lines into your php document where you want the number of connected users to be displayed


<?php

<?php 
echo $ppl;
?>

?>



That's the end of the tutorial. This could also be modified into a makeshift hit counter with a few modifications.. As always, fool around with it. Happy coding.
 

 
   Print this page

Top Sponsor
Symantec\'s Norton SystemWorks 2006
Sponsors
CA
Sponsors
AdWords Dominator 125*125
Advertisting


Affiliates
VertexTemplates PHPFreaks CodeWalkers StarGeek DevScripts CGI & PHP Scripts PHP CMS Free Templates

Shopping Rebates   Sell It 4 You   Flash Page Counters   Get Insured
GPS Tracking Service   Charity Donate Info   Web Site Hosting   VOIP Service

Privacy Policy | Links | Site Map | Advertising

All content on OxyScripts.com is (©)2002-2007

 
Powered by Adrastea - Version 1.0.0. Copyright © Rune Solutions, 2004-2005