请理解我目前没有数据库访问权限。
所以这就是我正在做的事情。我有一个我正在练习的课程。我有一个createaccount文件,它使用表单为用户获取用户名,密码和电子邮件。然后它将字段写入文本文件,并添加“非活动”字段。状态到文件,然后通过电子邮件向用户提供验证链接。当用户单击激活链接时,我需要该文件来检查它正在处理的用户名,然后在文本文件中搜索用户并将状态从非活动状态更改为活动状态。
不确定如何完成此操作,我的代码现在只是告诉我文件已被破坏。有没有比破坏和重新创建文件更好的方法呢?我是php新手,我确实意识到根本不建议使用文本文件,真正的用户不会使用这个脚本,只是我自己的个人练习才能使用表格。
让我知道你的想法,非常感谢任何帮助!
<?php
include 'sessions.php';
$userid = $_GET['newUserName'];
$Email = $_POST['newEmail'];
$Users = file("../writeable/users.txt");
for($i=0; $i < count($Users); $i++) {
$row = explode(",", $Users[$i]);
if ($row[$i]===$Email){
$userstring="$row[0]".","."$row[1]".","."$row[2]".","."active\n";
$_SESSION['active'] = TRUE;
}
}
//destroy the users.txt file
if (file_exists("../writeable/users.txt")){
if(unlink("../writeable/users.txt")){
echo "File destroyed, will now be recreated";
}
}
else
echo "<p>Unable to delete this file<p>";
$Userfile = fopen("../writeable/users.txt", "w+"); //recreate the file
$userstring = implode(",", $Users); //make the array into a string.
fwrite($Userfile, $userstring); //write the string userstring into userfile
if(!file_exists("../writeable/users.txt")){
echo "<p>This file was not written<p>";
}
?>
请保持温柔,我是新的:)
答案 0 :(得分:1)
我建议您更改保存文件中数据的方式。序列化可以帮助您,看看JSON。
这是一个独立的工作代码,您可以将其添加到任何名称的新文件中(不包含电子邮件,但一旦您明白了,就可以轻松添加。还有,记住打开文件以查看更改):
$('.modal-form').each(function () {
var formModal = $(this);
var formID = $(this).attr('data-form-id') || 1619;
var componentID = $(this).attr('data-componentID') || '';
var formRedir = $(this).attr('data-redirect') || false;
var formResult = $(this).attr('data-form-result') || "blank";
var formSucessUrl = $(this).attr('data-success-url') || '';
var formSuccessMsg = $(this).attr('data-sucess-msg') || false;
MktoForms2.loadForm("//app-ab13.marketo.com", "754-FXO-315", formID, function (form){
form.onSuccess(function(values, followUpUrl){
formModal.find(".modal-content").fadeOut('fast');
if (formRedir == "true") {
if (formResult == "blank") {
window.open(formSucessUrl, '_blank');
formModal.find(".modal.fade").modal('hide');
} else {
location.href = formSucessUrl;
}
} else if (formRedir =="false" && formSuccessMsg =="true") {
formModal.find(".modal-sucess-msg").fadeIn(100).delay(5000).fadeOut('slow').delay(600, function(){
formModal.find(".modal.fade").modal('hide');
formModal.on('hidden.bs.modal', function () {
formModal.find(".modal-content").show();
formModal.find(".modal-sucess-msg").hide();
});
});
}
return false;
});
});
});
使用此结构,您可以检查用户名和密码是否匹配,只是直接访问该值。 要添加新用户,这样的东西应该可以工作(不检查它是否存在):
<?php
// change to true to create file with example (change to false to keep changes)
$firstRun = true;
if(isset($_GET['key'])){
$file = "users.txt"; // original data in this one
$file2 = "before.txt"; // saves what was before the edition (just to check)
// 2 files just to see the difference
$key = $_GET['key']; // from email link
if($firstRun){
/* WITHOUT unique key from associative array - this might require loop through the elements
$data = array('username' => 'userD', 'password' => 'IamNOTencryptedX', 'active' => false,
'username' => 'userC', 'password' => 'IamNOTencryptedY', 'active' => false,
'username' => 'userB', 'password' => 'IamNOTencryptedZ', 'active' => false,
'username' => 'userA', 'password' => 'IamNOTencrypted1', 'active' => false,
'username' => 'userX', 'password' => 'IamNOTencrypted2', 'active' => false
);
*/
// WITH unique key (the username in this example - could be id) - direct access
$users = array('userD' => array('username' => 'userD', 'password' => 'IamNOTencryptedX', 'active' => false),
'userC' => array('username' => 'userC', 'password' => 'IamNOTencryptedY', 'active' => false),
'userB' => array('username' => 'userB', 'password' => 'IamNOTencryptedZ', 'active' => false),
'userA' => array('username' => 'userA', 'password' => 'IamNOTencrypted1', 'active' => false),
'userX' => array('username' => 'userX', 'password' => 'IamNOTencrypted2', 'active' => false)
);
file_put_contents($file, json_encode($users)); // serialize and save to file
$users = ""; // just to make sure, not really needed
}
if(file_exists($file)){ // better be safe than sorry
$fileContent = file_get_contents($file); // read it all from file
file_put_contents($file2, $fileContent); // create a 'backup' to file2
$users = json_decode($fileContent, true); // true for associative array. This will recreate the array from the beginning based on the string from file
if(isset($users[$key])){ // make sure it's in the file
// you can check if this user is already activated too
$users[$key]['active'] = true; // activate
file_put_contents($file, json_encode($users)); // save back to file.
echo "USER '".$users[$key]['username']."' ACTIVATED. <i>[redirection needed]</i>";
}else{
echo 'KEY \''.$key.'\' NOT FOUND.<br> <form> Easy fix -> KEY: <input type="text" name="key"><input type="submit" value="Activate">'; // just in case you get lost when testing, don't use this
}
echo "\n\n";
// if you are going to use the first $data declaration, it's something like this
/*
foreach($users as $k => $value){
if($value == $key){
$users[$k]['active'] = true; // change value
file_put_contents($file, json_encode($users));
break;
}
}
*/
}else{
// FILE NOT CREATED BEFORE
echo "FILE CREATED JUST NOW. IS THIS FIRST RUN?";
file_put_contents($file, "{}");
}
}else{
echo 'MISSING KEY.<br> <form> Easy fix -> KEY: <input type="text" name="key"><input type="submit" value="Activate">';// just in case you get lost when testing, don't use this
}
?>
我很抱歉,如果其中任何一个感觉太无法解释,但是当我输入另一个并且我失去动力时电源就会消失。
如果您不打算更改它,您的代码将看起来像这样(表面测试):
$newUsername = 'still2blue'; // say this is form data
$newPassword = 'still2blue';
$users[$newUsername] = array('username' => $newUsername, 'password' => $newPassword, 'active' => false);
请注意,您可以使用array_search来实现保存结果。