- #1
Z
zuger
Guest
Hallo zusammen ich möchte eine Wer ist online Funktion bauen mit PHP. Hat jemand vorschläge wie ich das anstellen könnte? Besten Dank!
Follow along with the video below to see how to install our site as a web app on your home screen.
Anmerkung: This feature currently requires accessing the site using the built-in Safari browser.
<?php
class UserOnline {
/**
* User Online-Klasse
* 22/07/2005 by conny
* Support ausschließlich über [url]http://www.wintotal-forum.de[/url]
*/
var $db_host = localhost;
var $db_user = xxxx;
var $db_pass = xxxx;
var $db_name = test;
var $db_table = useronline;
var $ttl = 600; // Wie lange wird der User als Online geführt? (in sec.)
var $ip; // IP des entfernten Rechners
var $user_count = 0; // Anzahl aller Online-User
var $error = false; // Fehlermeldung
/**
* Hauptfunktion
*/
function UpdateTable() {
/**
* Zuerst werden alle Datensätze aus der Tabelle geschmissen, deren ttl abgelaufen ist.
*/
$this->CleanTable();
/**
* Prüfung ob IP schon in der Tabelle als Online geführt wird
*/
if($this->CheckIP())
# Ist schon drinn, also Update
$this->UpdateIP();
else
# Ist noch nicht drinn, also Insert
$this->InsertIP();
# Neue Zählung durchführen
$this->CountUser();
}
/**
* Aktuelle Online-Statistik
*/
function CountUser() {
$sql = SELECT count(ip) AS c FROM . $this->db_table .;
if(!$resID = @mysql_query($sql)) {
$this->error = [CountUser()] - .$this->GetErrorCode(3).<span style='color:red'>.$sql.</span>;
return false;
}
$row = @mysql_fetch_array($resID);
$this->user_count = $row[0];
return true;
}
/**
* Prüfen ob die IP schon mit einer gültigen TTL in der Tabelle steht
*/
function CheckIP() {
$sql = SELECT count(ip) AS c FROM . $this->db_table . WHERE ip =->. $this->ip.';
if(!$resID = @mysql_query($sql)) {
$this->error = [CheckIP()] - .$this->GetErrorCode(3).<span style='color:red'>.$sql.</span>;
return false;
}
else {
$row = @mysql_fetch_array($resID);
if($row[0] == 1)
return true;
return false;
}
}
/**
* TTL einer bestehenden IP in der Tabelle erneuern
*/
function UpdateIP() {
$sql = UPDATE . $this->db_table . SET expire=. ($this->ttl + time()) . WHERE ip='. $this->ip .';
if(!@mysql_query($sql)) {
$this->error = [UpdateIP()] - .$this->GetErrorCode(3).<span style='color:red'>.$sql.</span>;
return false;
}
return true;
}
/**
* Neue Remote-IP in Tabelle registrieren
*/
function InsertIP() {
$sql = INSERT INTO . $this->db_table . SET ip='. $this->ip .', expire=. ($this->ttl + time()) .;
if(!@mysql_query($sql)) {
$this->error = [InsertIP()] - .$this->GetErrorCode(3).<span style='color:red'>.$sql.</span>;
return false;
}
return true;
}
/**
* Tabelle aufräumen
*/
function CleanTable() {
$sql = DELETE FROM . $this->db_table . WHERE expire < . time() .;
if(!@mysql_query($sql)) {
$this->error = [CleanTable()] - .$this->GetErrorCode(3).<span style='color:red'>.$sql.</span>;
return false;
}
return true;
}
/**
* Fehlermeldungen
*/
function GetErrorCode($n = ) {
if(!$n) return false;
$e[1] = Kein Connect zur Datenbank!;
$e[2] = Kann Datenbank <strong>.$this->db_name.</strong> nicht &ouml;ffnen!;
$e[3] = Fehler in SQL-Abfrage: ;
return $e[$n];
}
/**
* Datenbank Connect
*/
function Connect() {
if(!@mysql_connect($this->db_host, $this->db_user, $this->db_pass)) {
$this->error = $this->GetErrorCode(1);
return false;
}
if(!@mysql_select_db($this->db_name)) {
$this->error = $this->GetErrorCode(2);
return false;
}
return true;
}
/**
* Konstruktor
*/
function UserOnline() {
$this->ip = $_SERVER['REMOTE_ADDR'];
if($this->Connect()) {
$this->UpdateTable();
}
}
}
?>
<?php
require_once(online.class.php);
$_DB = new UserOnline();
if($_DB->error) die($_DB->error);
$_X = ($_DB->user_count > 1) ? sind : ist;
echo Es $_X .$_DB->user_count. User Online;
?>
--
-- Tabellenstruktur für Tabelle `useronline`
--
CREATE TABLE `useronline` (
`ip` varchar(20) collate latin1_german1_ci NOT NULL default->',
`expire` int(11) NOT NULL default->0'
);