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