(source code) self-made online text notepad, streamlined full version, support for carriage return, display 10 history records, display IP
Need to create a history folder, after make the .php
<!DOCTYPE html>
<html>
<head>
<title>plan</title>
</head>
<body>
<?php
$filePath = “document.html”; // Path to document file
$historyPath = “history/”; // path to the history folder
$maxHistoryCount = 10; // maximum number of history records
if ($_SERVER[“REQUEST_METHOD”] === “POST”) {
$content = $_POST[“content”];
//Convert newline characters to HTML newline tags
$content = nl2br($content);
// Save content to document file
file_put_contents($filePath, $content);
// Create a history filename, using the current timestamp as the filename
$historyFileName = time() . “.html”;
// Save history to history folder
file_put_contents($historyPath . $historyFileName, $content);
// Get the history file list
$historyFiles = glob($historyPath . “*.html”);
// If the number of history records exceeds the maximum limit, delete the oldest history file
if (count($historyFiles) > $maxHistoryCount) {
// Sort by file modification time
usort($historyFiles, function ($a, $b) {
return filemtime($a) – filemtime($b);
});
// Delete the oldest history file
unlink($historyFiles[0]);
}
// Redirect to the current page in order to refresh the content of the document
header(“Location: ” . $_SERVER[“PHP_SELF”]);
exit();
}
//Load content from document file
$content = file_exists($filePath) ? file_get_contents($filePath) : “”;
?>
<form method=”post” action=”<?php echo $_SERVER[“PHP_SELF”]; ?>”>
<textarea name=”content” style=”width: 100%; height: 200px;”><?php echo htmlspecialchars(str_replace(“<br />”, “\n”, $content)); ?></ textarea>
<br>
<input type=”submit” value=”save”>
</form>
<hr>
<h2>History</h2>
<ul>
<?php
// Get the history file list
$historyFiles = glob($historyPath . “*.html”);
// Sort by file modification time
usort($historyFiles, function ($a, $b) {
return filemtime($b) – filemtime($a);
});
// Display the links, time and IP address of the first 5 history files and the person who left the message
$historyFiles = array_slice($historyFiles, 0, $maxHistoryCount);
$ip = $_SERVER[‘REMOTE_ADDR’];
foreach ($historyFiles as $file) {
$fileName = basename($file);
$fileTime = date(“Y-m-d H:i:s”, filemtime($file));
echo ‘<li><a href=”‘ . $historyPath . $fileName . ‘”>’ . $fileName . ‘</a> (‘ . $fileTime . ‘) from ‘ . $ip . ‘</li> ‘;
&nb
sp; }
?>
</ul>
<h2>Plan</h2>
<div><?php echo $content; ?></div>
</body>
</html>
Finally, a new empty folder for history needs to be created.