Getting Password from .env File in PHP


I have a PHP bot which needs a password to login. How can I get it from the .env file?

I think it should be $_ENV['My variable']


1 Like

It wont exactly work like that. You need to install a package, like:

Then the $_ENV superglobal will work.


Neither of the approaches worked. I am getting an error saying none of the credentials (username and password) are correct.

Can you share some code?

This kind of seems like #coding-help because it seems like it is dealing with code, so I moved it there


This is the code for logging in-

$wikiusername = $_ENV['username'];
$wikipassword = $_ENV['password'];


I installed it with Composer. Should I delete the composer.lock file?

No. You should not delete the composer.lock file.

Don’t forget to require the packages using:

require 'vendor/autoload.php'
1 Like

It looks like my username is properly being accessed but the password is not since it is a MediaWiki bot. MediaWiki is telling me that there have been multiple failed login attempts to my account. :confused: