2015-10-16 91 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