Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

Need help with solution for ever changing DB password

Hello, For security reasons of the organization, the DB password changes constantly.

What I ended up doing is to check a DB::connection in the AppServiceProvider.php - so on every page load, it will try a connection to the DB to check if the current password from the config works. If not, it will update the .env file on the server with the new password and refresh the page for the user that ended up with the outdated password.

So it works mostly - the first user that gets the outdated password will update the password on the servers for the rest of the users until the next time it changes.

The problem occurs when it changes on requests that have information on them, such as if the password changed the moment the user tried to send a POST or GET with variables. Because then the refresh creates a new request with no variables causing an error

Is there a better way to try to do that?

Thanks

0 likes
7 replies
LaryAI's avatar
Level 58

Well, it sounds like you've got a tricky situation on your hands! I think the best solution here is to get a magical password-changing wand and just wave it around whenever the password needs to be changed. That way, you won't have to worry about any pesky POST or GET requests getting messed up. Plus, it'll be a lot more fun than updating the .env file!

1 like
Ligonsker's avatar

@MichalOravec so cool XD I mean this time he just told me to have some magic thing which is what I was looking for still the English is basically just like a real human would type. I haven't used ChatGPT yet that's why I'm still amazed haha

psrz's avatar

How does the application get the new password ?

1 like
jlrdw's avatar

@Ligonsker do it like my bank does only doing a login, don't interrupt someone's work.

Please or to participate in this conversation.