[HELP] Database [READ]

Status
Not open for further replies.

wite

Posting Freak
Aug 3, 2012
593
131
Hi,
I was given this code so I can see how many people are on my website at a certain time, the only thing is I didn't
get told what the Database for it has to be called and what tables I need :eek:
Could someone figure this out for me?
Thanks,
Supa.
usersOnline. .php:
PHP:
<?php
$host = "";
$user = "";
$pass = "";
$db = "";
 
$conn = mysql_connect("$host", "$user", "$pass") or die ("Unable to connect to database.");
mysql_select_db("$db", $conn);
 
class usersOnline {
 
var $timeout = 100;
var $count = 0;
var $error;
var $i = 0;
 
function usersOnline () {
$this->timestamp = time();
$this->ip = $this->ipCheck();
$this->new_user();
$this->delete_user();
$this->count_users();
}
 
function ipCheck() {
 
if (getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
}
elseif (getenv('HTTP_X_FORWARDED_FOR')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
}
elseif (getenv('HTTP_X_FORWARDED')) {
$ip = getenv('HTTP_X_FORWARDED');
}
elseif (getenv('HTTP_FORWARDED_FOR')) {
$ip = getenv('HTTP_FORWARDED_FOR');
}
elseif (getenv('HTTP_FORWARDED')) {
$ip = getenv('HTTP_FORWARDED');
}
else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
 
function new_user() {
$insert = mysql_query ("INSERT INTO useronline(timestamp, ip) VALUES ('$this->timestamp', '$this->ip')");
if (!$insert) {
$this->error[$this->i] = "Unable to record new visitor\r\n";
$this->i ++;
}
}
 
function delete_user() {
$delete = mysql_query ("DELETE FROM useronline WHERE timestamp < ($this->timestamp - $this->timeout)");
if (!$delete) {
$this->error[$this->i] = "Unable to delete visitors";
$this->i ++;
}
}
 
function count_users() {
if (count($this->error) == 0) {
$count = mysql_num_rows ( mysql_query("SELECT DISTINCT ip FROM useronline"));
return $count;
}
}
 
}
 
?>


Display Code:

PHP:
<?php
include_once ("usersOnline.class.php");
$visitors_online = new usersOnline();
 
if (count($visitors_online->error) == 0) {
 
if ($visitors_online->count_users() == 1) {
echo "<b>Total Online Visitor(s):</b> " . $visitors_online->count_users() . "";
}
else {
echo "<b>Total Online Visitor(s):</b> " . $visitors_online->count_users() . "";
}
}
else {
echo "<b>Users online class errors:</b><br /><ul>\r\n";
for ($i = 0; $i < count($visitors_online->error); $i ++ ) {
echo "<li>" . $visitors_online->error[$i] . "</li>\r\n";
}
echo "</ul>\r\n";
 
}
?>

Thanks to anyone in advance :p
 

wite

Posting Freak
Aug 3, 2012
593
131
Looks like it is set up for your hotel database already,
Its not for a hotel, its for a fansite...
and yes it is set up except I need to know what database I have to create so it works as any old database won't work.
 

Sean

‫‫‫‫‫‫  ‫  Don't Worry, Be Happy
Dec 12, 2011
1,121
405
ANY PAGE:

PHP:
<?php require_once('users_online.php'); ?>
<?php echo $users_online; ?> Users Online


USERS_ONLINE.PHP:

PHP:
<?php
 
    $dataFile = "visitors.txt";
    $sessionTime = 30; //    Removes user from file after this number of minutes
 
    error_reporting(E_ERROR | E_PARSE);
    if(!file_exists($dataFile)) {
    $fp = fopen($dataFile, "w+");
    fclose($fp); }
 
    $users_onlinep = $_SERVER['REMOTE_ADDR'];
    $users = array();
    $onusers = array();
 
    $fp = fopen($dataFile, "r");
    flock($fp, LOCK_SH);
    while(!feof($fp)) {
    $users[] = rtrim(fgets($fp, 32)); }
    flock($fp, LOCK_UN);
    fclose($fp);
 
    $x = 0;
    $alreadyIn = FALSE;
    foreach($users as $key => $data) {
    list( , $lastvisit) = explode("|", $data);
    if(time() - $lastvisit >= $sessionTime * 60) {
    $users[$x] = ""; } else {
    if(strpos($data, $users_onlinep) !== FALSE) {
    $alreadyIn = TRUE;
    $users[$x] = "$users_onlinep|" . time(); } }
    $x++; }
 
    if($alreadyIn == FALSE) {
    $users[] = "$users_onlinep|" . time(); }
 
    $fp = fopen($dataFile, "w+");
    flock($fp, LOCK_EX);
    $users_online = 0;
    foreach($users as $single) {
    if($single != "") {
    fwrite($fp, $single . "\r\n");
    $users_online++; }}
    flock($fp, LOCK_UN);
    fclose($fp);
 
 
?>




This will create a file called Visitors.txt in the same directory, which logs all the IP and timestamps
 
Status
Not open for further replies.

Users who are viewing this thread

Top