Php 每次用户尝试创建新文件时都会收到“die”消息
我正在做一个PHP任务,收集关于用户的信息并将其存储在一个文件中,具体取决于用户是否订阅免责声明:我知道在数据库中使用mySQL做这件事更好,但我们还没有学会,被告知要使用PHP。我有单选按钮来决定是订阅还是取消订阅。给你Php 每次用户尝试创建新文件时都会收到“die”消息,php,file,radio-button,subscribe,Php,File,Radio Button,Subscribe,我正在做一个PHP任务,收集关于用户的信息并将其存储在一个文件中,具体取决于用户是否订阅免责声明:我知道在数据库中使用mySQL做这件事更好,但我们还没有学会,被告知要使用PHP。我有单选按钮来决定是订阅还是取消订阅。给你 <input type="radio" name="sub" value="subscribe" checked="checked" /> <input type="radio" name="sub" value="unsubscribe" /> 有
<input type="radio" name="sub" value="subscribe" checked="checked" />
<input type="radio" name="sub" value="unsubscribe" />
有人能帮我理解为什么每次我都像应该的那样填写所有必填字段时都会收到错误消息吗?谢谢。用用户发布的名称创建文件是一个非常糟糕的主意,这是一个安全漏洞,而且-在哪个文件夹中创建此文件?我知道这是一个糟糕的主意,这就是为什么我在免责声明中说最好在数据库或mySQL中这样做。。。。如何指定文件的存储位置?fopen第一个参数用于说明文件的存储位置我以为那是文件名?所以我需要创建一个名为“
myfile
”的文件夹吗?您需要提供文件的绝对路径,比如/tmp/myfolder/$EmailAddress.txt
$Name = htmlspecialchars($_POST['name']);
$EmailAddress = htmlspecialchars($_POST['emailaddress']);
$Action = $_POST['sub'];
if($EmailAddress == "") {
print "<p>Error. No email address. Please try again.</p>";
$move = 0;
} else {
$move = 1;
}
if($move) {
// If the user subscribes, make a file for them.
if($Action == "subscribe") {
// Create a file for each user saved as their email address.
$myfile = fopen($_POST['emailaddress'] . ".txt", "w") or die("Unable to create file!");
$txt = "Name: " . $_POST['name'] . "\n"; // Add the name to the file
fwrite($myfile, $txt);
$txt = "Email Address: " . $_POST['emailaddress'] . "\n"; // Add the email address to the file
fwrite($myfile, $txt);
// Check which preferences are set. Add to file if they are checked.
if(isset($_POST['compositions'])) {
$txt = "Preference: " . $_POST['compositions'] . "\n"; // Add the first preference to the file if checked
fwrite($myfile, $txt);
}
if(isset($_POST['marchingband'])) {
$txt = "Preference: " . $_POST['marchingband'] . "\n"; // Add the second preference to the file if checked
}
if(isset($_POST['projects'])) {
$txt = "Preference: " . $_POST['projects'] . "\n"; // Add the third preference to the file if checked
}
if(isset($_POST['events'])) {
$txt = "Preference: " . $_POST['events'] . "\n"; // Add the fourth preference to the file is checked
}
fclose($myfile);
print "<p>Thank you for subscribing!</p>";
} else {
// If a user unsubscribes, delete their file.
unlink($_POST['emailaddress'] . ".txt");
print "<p>You have successfully unsubscribed.</p>";
}
}