PHP激活用户


PHP activating a user

请理解我现在没有数据库访问权限。

这就是我所做的。我在做一个程序,只是为了练习。我有一个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>";
    }
?>

请温柔一点,我又是新人

我建议您更改在文件中保存数据的方式。序列化可以帮助你,看看JSON。

下面是一段独立的工作代码,您可以将其放入新文件中,并使用任何名称(不包括电子邮件,但一旦您了解了这个想法,就很容易添加。另外,请记住打开文件以查看更改):

<?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.

来实现保存结果。