2
0

Compare commits

...

2 Commits

Author SHA1 Message Date
Schuledaniel
1c7e90e8c5 Update Zweits Jahr/README.md: add task name 2025-11-19 13:49:14 +01:00
Schuledaniel
ed17579448 task: log acces in txt file 2025-11-19 13:47:58 +01:00
3 changed files with 63 additions and 1 deletions

View File

@@ -5,8 +5,13 @@
*Wir warscheinlich nie aktuell aber egaaaal* *Wir warscheinlich nie aktuell aber egaaaal*
### 18-11-25 - PHP ### 19-11-25 - PHP
- Übung Datei lesen/schreiben (Log Access)
### 18-11-25 - PHP
- Datein lesen - Datein lesen
### Davor ### Davor

View File

@@ -0,0 +1,3 @@
Count | Date | IP
1 | 1763556283 | 127.0.0.1
1 | 1763556285 | 127.0.0.1

View 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);