我建议你改变你正在保存的文件中的数据的方式。序列化可以帮助你,看看JSON。
这里是一个独立运行的代码,你可以把一个新文件与任何名称(不包括电子邮件,但一旦你的想法,很容易增加。此外,remeber打开文件以查看更改):
<?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);
我很抱歉,如果任这种感觉太原因不明,但动力去了,当我键入其他我和一个人失去了动力。
如果你不打算改变它,你的代码必须是这个样子(表面上测试):
<?php
include 'sessions.php';
$fileName = "../writeable/users.txt";
$userid = $_GET['newUserName'];
$Email = $_GET['newEmail']; // changed to get
$Users = file($fileName);
$userstring = "";
$found = false;
for($i=0; $i < count($Users); $i++) {
$row = explode(",", $Users[$i]); // the name row here is wrong, the var $Users is equivalent to rows, this represents each 'column'
foreach($row as $k => $c){
if($found){ // since it's unique
$userstring .= $row[0].",".$row[1].",".$row[2].",".$row[3].""; // the \n is already part of 'inactive'
break;
}elseif($c===$Email){
$userstring .= $row[0].",".$row[1].",".$row[2].",active\n";
$_SESSION['active'] = TRUE;
$found = true;
break; // no need to keep checking if it's unique
}elseif($k == count($row)-1){ // if you checked all the 'columns' of that row and didn't find the match, add it without active
$userstring .= $row[0].",".$row[1].",".$row[2].",".$row[3].""; // the \n is already part of 'inactive'
}
}
}
//destroy the users.txt file
if (file_exists($fileName)){
if(unlink($fileName)){
echo "File destroyed, will now be recreated";
}
}
else
echo "<p>Unable to delete this file<p>";
$Userfile = fopen($fileName, "w+"); //recreate the file
// removed
//$userstring = implode(",", $Users); //make the array into a string.
fwrite($Userfile, $userstring); //write the string userstring into userfile
if(!file_exists($fileName)){
echo "<p>This file was not written<p>";
}
?>
请注意,您可能能够实现将结果与array_search。
@MonkeyZeus我明白了,如果你阅读我的文章,说我知道这不是完成的方式,但这是为了我的练习,我现在没有数据库访问权限。请寻找有意义的贡献。 – still2blue
对于这种基于文件的模式,最好通过创建文件的临时副本,对该文件执行任何更新,对该副本进行更改,删除原始文件,然后将副本重命名为原始文件名。当然,还有一些并发访问问题需要考虑,但考虑到你理解这不是一个理想的解决方案,而且这是为了实践,我不会太在意这方面的问题。 –
你可以在'users.txt'文件中添加更多'字段'吗?如果是这样,只需在文件内添加一个唯一的字符串给需要激活的用户,并创建激活链接作为get参数。 – FirstOne