<?php
function writeData()
{  
  $time = time();
  $addr = $_SERVER['REMOTE_ADDR'];
  if(isSet($_SERVER['HTTP_USER_AGENT'])){
    $agent = $_SERVER['HTTP_USER_AGENT'];
  }
  else{
    $agent = "Nieznany";
  }

  $query = "INSERT INTO stats (time, addr, agent) VALUES(";
  $query .= "'$time', '$addr', '$agent')";

  if(!$result = @mysql_query($query)){
    //echo("Problem z bazą danych: odrzucone zapytanie");
    return false;
  }
  else{
    return true;
  }
}

function readData($ile, $delimiter)
{
  $query = "SELECT * FROM stats ORDER BY time DESC LIMIT $ile";
  if(!$result = @mysql_query($query)){
    //echo("Problem z bazą danych: odrzucone zapytanie");
    return false;
  }
  $str = "";
  while(($row = @mysql_fetch_row($result)) != false){
    $str .= date("Y-m-d G:i", $row[0]) . " ";
    $str .= $row[1] . " ";
    $str .= $row[2] . $delimiter;
  }
  return $str;
}

function getAndWriteData($ile, $delimiter)
{
  if(!$link = @mysql_connect("localhost", "php", "test")){
    //echo("Problem z połączeniem z bazą danych");
    return;
  }
  if(!@mysql_select_db("testphp")){
    //echo("Problem z wyborem bazy testphp");
    @mysql_close($link);
    return;
  }

  $dane = readData($ile, $delimiter);
  writeData();

  @mysql_close($link);
  return $dane;
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1250">
<title>Lista odwiedzin</title>
</head>
<body>
<div>
<?php
echo getAndWriteData(10, "<br />");
?>
</div>
</body>
</html>
