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

时间:2016-04-17 20:56:46

标签: php

对于我的网站,您可以访问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>

1 个答案:

答案 0 :(得分:0)

因此,阅读您之前关于寻找一种简单的方法来收集数据而不需要安全性的评论,我建议现在将其保存在文本文件中,以后可能会使用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()的工作原理。放置&#34; w&#34;在第二个参数中意味着该函数将使用您提供的信息创建一个新文件,如果该文件已经存在,它将重写它,这意味着文件中存在的任何先前信息都将消失。出于这个原因,我使$ 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);