2015-10-16 85 views
1

请理解,我现在没有数据库访问权限。PHP激活用户

所以这就是我正在做的。我有一个我正在做的练习。我有一个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>"; 

    } 

?> 

请温柔,我再次新:)

+1

@MonkeyZeus我明白了,如果你阅读我的文章,说我知道这不是完成的方式,但这是为了我的练习,我现在没有数据库访问权限。请寻找有意义的贡献。 – still2blue

+0

对于这种基于文件的模式,最好通过创建文件的临时副本,对该文件执行任何更新,对该副本进行更改,删除原始文件,然后将副本重命名为原始文件名。当然,还有一些并发访问问题需要考虑,但考虑到你理解这不是一个理想的解决方案,而且这是为了实践,我不会太在意这方面的问题。 –

+0

你可以在'users.txt'文件中添加更多'字段'吗?如果是这样,只需在文件内添加一个唯一的字符串给需要激活的用户,并创建激活链接作为get参数。 – FirstOne

回答

1

我建议你改变你正在保存的文件中的数据的方式。序列化可以帮助你,看看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

+0

对不起,我对php的知识相当有限。因此,当我的电子邮件发送给用户时,它具有激活链接和用户名。在我的激活页面上,我首先获取用户名(它已在创建帐户页面上验证为唯一用户名,并未被其他人使用)。然后,我需要通过一个数组并存储所有在users.txt文件中查找信息,查找用户引用并将其状态更改为非活动状态,然后销毁原始文件,然后创建具有新状态的新文件,然后将其指向登录页面。你能展示示例代码吗? – still2blue

+0

对不起,让我清除一些东西。你是否只是将用户部分改为'active'而不必读取和重写其他所有内容?我可能误解了你的问题。 @ still2blue – FirstOne

+0

让我试着详细说明。我的表单需要用户输入,每行写入一个文本文件,如下所示:用户名,hashpass,电子邮件,不活动。因此,使用来自电子邮件链接的get参数,用户点击以检查我正在处理的用户名,我希望通过并保存所有信息,同时查找特定用户,找到他时,将非活动状态更改为活动状态,然后销毁原始文件并用所有新信息重写。从这里他们将被重定向到一个登录页面,在那里它会验证它们。欣赏你的帮助btw – still2blue