gpt4 book ai didi

使用txt文件的PHP评论框

转载 作者:行者123 更新时间:2023-12-05 05:28:10 25 4
gpt4 key购买 nike

我需要使用 txt 文件或任何其他可以被动查询的内容来制作评论框,而无需使用数据库服务器。由于我是 PHP 编程的新手,第一个想法是使用文本文件。就我能想到的逻辑而言,一般来说,实现这一点的代码是:

    <html>
<head></head>
<body>
<form method = "post">
<textarea name = "txt" cols = "25" rows = "5">
Place your comment here ...
</textarea><br><br>
<input type = "submit" value = "Submit" onclick = "<?php
$com = $_POST["txt"];
$file = fopen("inrg.txt", "a");
fwrite($file, "<br>");
for($i=0; $i <= strlen($com) - 1; $i++)
{
fwrite($file, $com[$i]);
if($i % 37 == 0 && $i != 0)
fwrite($file, "<br/>");
}
fwrite($file, "<br>------------------------------------------");
fclose($file);
?>">
<br>
</form>
<font face = "Times New Roman"><b><p>Textul introdus este: </p></b></font>
<font face = "Comic Sans MS" color = "red" size = "2" >
<?php
$file = fopen("inrg.txt", "r");
echo fread($file, filesize("inrg.txt"));
fclose($file);
?>
</font>
</body>
</html>

还没有什么特别的,它确实需要在美学方面进行一些改进。问题是在我在评论框中提交内容后,它会正确显示,但如果我在网络浏览器中重新加载,最后发布的评论会在我重新加载页面时再次发布。另外,如果 PHP 有办法让最初的“在这里放置您的评论......”消失

最佳答案

    <!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Example document</title>
</head>
<body>
<?php
// the relative path to the file
$fname = "theFile.txt";
// read in the file if present
if(file_exists($fname)) $txt = file_get_contents($fname);

// if the user pushes the submit button
if(isset($_POST["txt"])){
$txt = $_POST["txt"]; // get the entered content
file_put_contents($fname,$txt); // write the content to the file
}
?>
<form method="post" action="#">
<textarea name = "txt" cols = "25" rows = "5">
<?php echo $txt; ?>
</textarea><br />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>

脚本所在的文件夹必须可由 PHP (=WebServer) 写入。这个脚本很关键,没有针对跨端脚本黑客的安全措施。可能存在换行问题。

关于使用txt文件的PHP评论框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13501510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com