我可以让我的post.php永久保存数据吗


Can I make my post.php save the data permanantly?

对于我的网站,您可以访问http://desire.site88.net在最底部,你会看到我的表格。当你完成表格并按下提交按钮时,表格会将数据提交到desire.site88.net/post.php。我想知道的是如何使其永久化?当用户向post.php提交数据时,我希望它保持不变。不寻找任何安全或不可破解的东西,只是我可以用来招募会员的东西。这是我的代码

<?php
$username = $_POST['username']; 
$email = $_POST['email']; 
$message = $_POST['message']; 
// what it will say down below
echo $username. ' has an email of </br>'; 
echo $email. ' and wants to join because </br>'; 
echo $message. '</br></br>'; 

<form method="post" action="test.php"> <div class="row 50%"> <div class="6u 12u(mobile)"><input type="text" name="username" placeholder="Username" /></div> <div class="6u 12u(mobile)"><input type="email" name="email" placeholder="Email" /></div> </div> <div class="row 50%"> <div class="12u"><textarea name="message" placeholder="Application" rows="6"></textarea></div> </div> <div class="row"> <div class="12u"> <ul class="actions"> <li><input type="submit" value="submit" /></li> </ul> </div> </div> </form>

因此,阅读您之前关于寻找一种无需安全性即可收集数据的简单方法的评论,我建议暂时将其保存在文本文件中,稍后可能使用XML进行保存,您可以稍后对此进行研究。

保存到文本文件中的代码:

$filePath = $username."-".time().".txt";
$myFile = fopen($filePath, "w");
fwrite($myFile, ("username: ".$username."'n"));
fwrite($myFile, ("email: ".$email."'n"));
fwrite($myFile, ("message: ".$message."'n"));
fclose($myFile);

该代码每次保存时都会保存一个具有唯一名称的文件,并且该文件将与php页面位于同一目录中。

让我知道这对你是否有效,或者你是否有任何疑问:)

编辑:首先解释函数fopen()的工作原理。将"w"放在第二个参数中意味着该函数将使用您提供的信息创建一个新文件,如果该文件已经存在,它将重新写入该文件,这意味着该文件中以前存在的任何信息都将消失。出于这个原因,我使$filePath是唯一的,这样就不会发生覆盖。我现在将更进一步,将日志包含在根文件夹之外的一个新的单独文件中,以增加安全性:

//++++ path obtained to your root folder
$root_directory_path = $_SERVER['DOCUMENT_ROOT'];
//++++ creating the path for the logs in a new folder outside
//++++ the root director
$filePath = $root_directory."/../my_logs/".$username."-".time().".txt";
//++++ starting the creation of the file
$myFile = fopen($filePath, "w");
//++++ inputing information into the file
$inputString = "username: ".$username."'n";
fwrite($myFile, $inputString);
$inputString = "email: ".$email."'n";
fwrite($myFile, $inputString);
$inputString = "message: ".$message."'n";
fwrite($myFile, $inputString);
//++++ closing the file / finalizing the creation of the file
fclose($myFile);

我访问了你的网站,似乎你对网站授予你的权限有问题。如果你仍然希望在根目录中使用文本文件,你可以按照下面的代码操作,但要注意,任何人都可以查看注册的用户,因为信息保存在rood目录的子文件夹中,所以我建议你转换到一个安全的数据库:

$filePath = "/myLogs/".$username."-".time().".txt";
$myFile = fopen($filePath, "w");
fwrite($myFile, ("username: ".$username."'n"));
fwrite($myFile, ("email: ".$email."'n"));
fwrite($myFile, ("message: ".$message."'n"));
fclose($myFile);