Getting Password from .env File in PHP

,

Hello,
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']

Docs: https://www.php.net/manual/en/reserved.variables.environment.php

1 Like

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

Then the $_ENV superglobal will work.

2 Likes

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

2 Likes

This is the code for logging in-

<?php
$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:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.