drf65218 2018-09-07 11:39
浏览 34
已采纳

如何在另一个文件的变量中向数组添加数据?

i have the following variable with an array in config.php:

$user_password = array(
'admin' => 'phpass:!2a!09!0VacTRBCSGujva0D474.Ce3XI6hEjb80/4VTYKGF0LFx9sIY/BdA.',
'test' => 'phpass:!2a!09!WFA38xA3BBdT.5nJVVMmBOQVAkyuAC/RluiN0ekNPAr0w6d4YBZK2',
'test2' => 'phpass:!2a!09!ZiysPdYPcL3tKpUiVFZLlOVw.l/E4paN6LVDCrRNCsResNex1NUmK' 
);

I would like to add a new user from my login page, how would i go about adding new users in the config.php $user_password array ?

Im not really sure how to start.

should i include the file or use require_once then edit the array and in config.php ..?

EG:

if (isset($_REQUIRE['login-btn'])) {
    $username = $_REQUEST['user'];
    $passwd = $_REQUEST['passwd'];
    if (isset($username) && isset($passwd)) {
        //code to add user an passwd to the config variable $user_passwd array
        }
}

</div>
  • 写回答

1条回答 默认 最新

  • duanseci1039 2018-09-07 12:11
    关注

    The current methodology is quite cumbersome and unnecessarily restricted. You should definitely switch it over to using JSON for easier manipulation and storage:

    config.php

    $user_password = json_decode(file_get_contents('/path/to/users.json'));
    

    users.json

    DO NOT PLACE THIS FILE IN A PUBLICLY ACCESSIBLE LOCATION WHICH WOULD ALLOW ANY VISITOR TO DOWNLOAD IT

    {"admin":"phpass:!2a!09!0VacTRBCSGujva0D474.Ce3XI6hEjb80\/4VTYKGF0LFx9sIY\/BdA.","test":"phpass:!2a!09!WFA38xA3BBdT.5nJVVMmBOQVAkyuAC\/RluiN0ekNPAr0w6d4YBZK2","test2":"phpass:!2a!09!ZiysPdYPcL3tKpUiVFZLlOVw.l\/E4paN6LVDCrRNCsResNex1NUmK"}
    

    Adding new user

    $user_password[$username] = $passwd;
    file_put_contents('/path/to/users.json', json_encode($user_password));
    

    Note:

    Without using a database, this is the proper way to handle the current situation. However, if you start getting tens of thousands of users then this method will only get slower and slower.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染