Allora ho uno script che al momento del login mi visualizza l IP di ogni singolo utente. Io vorrei mettere affianco a ciascun IP il nick di chi effettua il login. Il login è per nick senza reg.
login.php
Codice PHP:
<html>
<div align="center"><form method="post" action="online.php" id="formlogin">
<label for="username">Username</label>
<input type="text" name="nick" id="username" />
<left><input type="submit" name="invia" id="login" value="ENTRA" />
</form>
</div>
</html>
online.php
Codice PHP:
<?php include 'configurazione/db.php';
session_start();
ob_start();
$nick = htmlspecialchars($_POST['nick']);
if($nick != "Nick"){
$_SESSION['Nick'] = $nick;
}
?>
<?php
$conn = mysql_connect('localhost', 'howey', '') or die(mysql_error());
$selezione_db = mysql_select_db('my_howey', $conn) or die(mysql_error());
require_once('online.php');
$onlineIP = New OnlineUsers;
?>
<html>
<head>
<title>Utenti Online</title>
</head>
<body>
<?php
// number
$numero = $onlineIP->get_count_online();
echo '<h3>IP Utenti collegati: '. $numero .'</h3>';
if($numero>=1){
echo '<ul>';
// ip online
$log = $onlineIP->get_online_key();
foreach($log as $log){
echo '[*]' . $log . '';
}
echo '[/list]';
}
?>
</body>
</html>
<?php
Class OnlineUsers{
protected $key;
protected $minute_finish = 10;
public function __construct(){
$this->key = $this->get_key(); // otteniamo la key
$this->clear_table(); // eliminiamo gli utenti "scaduti"
if(!$this->isset_key()){ // non esiste la key
$this->insert_key(); // allora la inseriamo
}
else{
$this->update_data_key(); // altrimenti aggiorniamo il timestamp
}
}
protected function isset_key(){
$query = "SELECT * FROM online_users WHERE key_user='" .mysql_real_escape_string($this->key). "'";
$result = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($result)>=1){
return TRUE;
}
return FALSE;
}
protected function update_data_key(){
$query = "UPDATE online_users SET data_last_log=NOW() WHERE key_user='" . mysql_real_escape_string($this->key) . "'";
$result = mysql_query($query) or die(mysql_error());
return TRUE;
}
protected function insert_key(){
$query = "INSERT INTO online_users SET key_user='" . mysql_real_escape_string($this->key) . "', data_last_log=NOW()";
$result = mysql_query($query) or die(mysql_error());
return mysql_insert_id();
}
protected function clear_table(){
$query = "DELETE FROM online_users WHERE data_last_log<=DATE_SUB( NOW() , INTERVAL ". intval($this->minute_finish) ." MINUTE )";
$result = mysql_query($query) or die(mysql_error());
return TRUE;
}
protected function get_key(){
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'undefined';
return $ip;
}
public function get_count_online(){
$query = "SELECT COUNT(*) AS num_user FROM online_users ORDER BY data_last_log ASC";
$result = mysql_query($query) or die(mysql_error());
$fetch = mysql_fetch_array($result);
return $fetch['num_user'];
}
public function get_online_key(){
$query = "SELECT key_user FROM online_users ORDER BY data_last_log ASC";
$result = mysql_query($query) or die(mysql_error());
$online = array();
while($row = mysql_fetch_array($result)){
$online[] = stripslashes($row['key_user']);
}
return $online;
}
}
if(isset($_POST['nick']) && !empty($_POST['nick']) ){
$_SESSION['nick'] = htmlentities(addslashes($_POST['nick']));
}
if(isset($_SESSION['nick'])){
}
?>
tabella DB
codice:
CREATE TABLE IF NOT EXISTS `online_users` (
`nick` text NOT NULL,
`id_log` int(11) NOT NULL AUTO_INCREMENT,
`key_user` varchar(255) NOT NULL,
`data_last_log` datetime NOT NULL,
PRIMARY KEY (`id_log`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
db.php
Codice PHP:
<?php
/* CONNESSIONE AL DATABASE */
$mysql['host'] = "localhost";
$mysql['user'] = "howey";
$mysql['pass'] = "";
$database = "my_howey";
mysql_connect($mysql['host'], $mysql['user'], $mysql['pass']) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
$time_refresh = 0;
?>