task: log acces in txt file
This commit is contained in:
@@ -5,8 +5,13 @@
|
||||
*Wir warscheinlich nie aktuell aber egaaaal*
|
||||
|
||||
|
||||
### 18-11-25 - PHP
|
||||
### 19-11-25 - PHP
|
||||
|
||||
- Übung Datei lesen/schreiben
|
||||
|
||||
|
||||
### 18-11-25 - PHP
|
||||
|
||||
- Datein lesen
|
||||
|
||||
### Davor
|
||||
|
||||
3
Zweites Jahr/log_access/access_log.txt
Executable file
3
Zweites Jahr/log_access/access_log.txt
Executable file
@@ -0,0 +1,3 @@
|
||||
Count | Date | IP
|
||||
1 | 1763556283 | 127.0.0.1
|
||||
1 | 1763556285 | 127.0.0.1
|
||||
54
Zweites Jahr/log_access/log_access.php
Normal file
54
Zweites Jahr/log_access/log_access.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Document</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
<?php
|
||||
/*
|
||||
|
||||
Arbeitsauftrag:
|
||||
Erstellen Sie ein PHP-Skript namens „log_access.php”, das jeden Aufruf der Seite in der Textdatei „access_log.txt” speichert.
|
||||
Beim Aufruf der Seite soll das Skript das aktuelle Datum und die aktuelle Uhrzeit erfassen.
|
||||
- das aktuelle Datum und die aktuelle Uhrzeit erfassen.
|
||||
- die IP-Adresse des Clients ermitteln (Tipp: $_SERVER)
|
||||
- diese Informationen in die Datei access_log.txt im selben Verzeichnis schreiben.
|
||||
|
||||
Bonus:
|
||||
Geben Sie im Browser zusätzlich an, wie viele Zugriffe insgesamt schon protokolliert wurden.
|
||||
Tipp: Schauen Sie sich die Funktionen file() und readfile() an oder nutzen Sie fread() wie bisher.
|
||||
|
||||
*/
|
||||
|
||||
$now = time(); // current time
|
||||
$userIP = $_SERVER['REMOTE_ADDR'] ; // users ip
|
||||
|
||||
$file = "access_log.txt";
|
||||
$fp = fopen($file, 'a') or die ("Cannot open file"); // open file for writing
|
||||
|
||||
if (countLines($file) < 2) {
|
||||
$txt = "Count | Date | IP\n";
|
||||
fwrite($fp, $txt);
|
||||
$count = countLines($file);
|
||||
$txt = "$count | $now | $userIP\n";
|
||||
fwrite($fp, $txt);
|
||||
echo countLines($file) -1;
|
||||
} else {
|
||||
$count = countLines($file) - 1;
|
||||
$txt = "$count | $now | $userIP\n";
|
||||
fwrite($fp, $txt);
|
||||
echo countLines($file) - 1;
|
||||
}
|
||||
|
||||
function countLines($currentFile){
|
||||
return count(file($currentFile));
|
||||
}
|
||||
|
||||
fclose($fp);
|
||||
Reference in New Issue
Block a user