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.

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

报告相同问题?

悬赏问题

  • ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
  • ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)
  • ¥15 yolov9的训练时间
  • ¥15 二叉树遍历没有报错但无法正常运行
  • ¥15 在linux系统下vscode运行robocup3d上场球员报错
  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动