【问题标题】:How to write a function for changing password of exact user in txt file如何编写一个函数来更改txt文件中确切用户的密码
【发布时间】:2018-11-01 11:02:21
【问题描述】:

我有三个输入字段一个用于电子邮件另一个密码第三个是更改密码的字段但是在 txt 文件中我已经编写了代码但它不起作用我应该添加到我的代码中以更改确切用户的当前密码这在txt文件中已经不仅仅是一个,为什么我需要内爆和爆炸功能

 <?php
    if(isset($_POST['change_log'])){
    $fread2=[];
        $email =$_POST['email'];
        $old_password =$_POST['password'];
        $new_password =$_POST['new_password'];
        $file = fopen( "data.txt","w",FILE_SKIP_EMPTY_LINES|FILE_IGNORE_NEW_LINES );
        //  $file=fopen("data.txt","w") or die("Cannot open"); 
            if(filesize('data.txt')>0){
                $read=fread($file,filesize('data.txt'));
                $fread2=explode(" ",$read);
                fclose($file);

            }

            for($i=0;$i<count($fread2);$i++){
                if($fread2[$i] == $email){
                    $fread2==$email;
                    $i++;
                    if($fread2[$i]== $old_password){
                        $fread2==$new_password;
                    }
                //  $fread2 = implode(' ',$fread2);
                    $file = fopen("newfile.txt", "w") or die("Unable to open file!");
                    fwrite($file, $fread2);
                    fclose($file);

                }
                else{
                    echo 'Password does  not match ';
                }
                break;
            }
        }
    ?>

【问题讨论】:

  • but it not works - 好的,你期待什么,会发生什么。请修改您的问题。
  • 另外,data.txt 文件的布局是什么?一个用户有多少个值?为什么不使用带有命名键的 JSON?
  • 这是我们的menbtor提出的一个合乎逻辑的问题
  • 我不明白你的意思。这是你的任务吗?
  • 也许你应该通读这里。 stackoverflow.com/help/asking 它们是关于你应该做什么以及不应该在堆栈溢出时问什么的指导方针。您在这里要问的是,我们为您编写一个 for 函数,但这不是 Stack Overflow 的用途。首先,了解您要做什么,自己尝试一下,然后,当您遇到更具体的问题时,提出这个问题。

标签: php algorithm loops logic core


【解决方案1】:
public function passAddOrChange($user, $pass)
{
    // $this->storage <--- path to file with <user:pass> data
    // json_last_error() != true, file_get_content() != false; 
    $passwords = (array)json_decode(file_get_contents($this->storage));
    $passwords[$user] = $pass;
    file_put_contents($this->storage, json_encode($passwords));
}

【讨论】:

    【解决方案2】:

    此功能可以满足您的要求,并添加了一些附加功能,如果您希望将来扩展代码,例如更新数据库,这将比存储在文本文件中容易得多。

    p>

    此答案假定您的 data.txt 文件具有如下结构:

    user1.email@site.com password user2.email@site.com otherpassword
    

    代码如下:

    <?php
    
    //Your variables
    $email = $_POST['email'];
    $old_password = $_POST['password'];
    $new_password = $_POST['new_password'];
    
    //Call the function to change the password
    changePassword( $email, $old_password, $new_password );
    
    function changePassword( $email, $old_password, $new_password){
    
        //The status will be updated to true if successful.
        //This function returns boolean.
        $status = false;
    
        //The new password can't have spaces
        if( strpos( $new_password, " " ) !== false ){
            return $status;
        }
    
        //Read the file.
        $file = fopen( "data.txt", "r+", FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES );
        $read = fread( $file, filesize( "data.txt" ));
    
        //Close the file
        fclose( $file );
    
        //Explode the data. There are two values which make up a user, so chunk into users.
        $data = array_chunk( explode( " ", $read ), 2 );
    
        //The names of each element (purely to make code easier)
        $keys = array(
                "email",
                "password"
            );
    
        //For every user in the data file.
        foreach( $data as $userID=>$user ){
            //Combine the user data with keys
            $user = array_combine( $keys, $user );
    
            //If the current credentials are correct
            if( $user["email"] == $email && $user["password"] == $old_password ){
                //Change the password on this user
                $user["password"] = $new_password;
    
                //Update the user record
                $data[$userID] = $user;
    
                //We have changed the user password, change status
                $status = true;
    
                //Since we updated the user, we don't need to proceed through the loop
                break 1;
            }
        }
    
        //This is the output data
        $outputData = array();
    
        //Since we broke the array into users, we need to put it back into a 1d array
        foreach($data as $userID=>$user){
            $outputData[] = implode( ' ', $user );
        }
    
        //Implode the array so we can put it back in the file
        $outputData = implode( " ", $outputData );
    
        //Write the data to the file.
        file_put_contents( "data.txt", $outputData );
    
        //Return the status, which will be true.
        return $status;
    }
    

    【讨论】:

    • @recv 如果这对您有用,请标记为答案。
    • 我应该在哪里做呢?
    • 我的答案左边的勾号。点击它。
    猜你喜欢
    • 1970-01-01
    • 2016-04-07
    • 2020-11-03
    • 2017-12-14
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    相关资源
    最近更新 更多