There’s no point in using putenv
, If you’re gonna do this, you might as well throw it all in one array
or object
, something like:
config.php
the return
is recognized in the include
<?php
return (object) array(
'host' => 'localhost',
'user' => 'root',
'pass' => 'meupassword',
'db' => 'meubanco',
);
php page.
$config = require 'config.php';
$con = new mysqli($config->host, $config->user, $config->pass, config->banco);
The issue of security
Maybe that story you heard getenv
be safer is to set up refer to setting up such data outside the .php
, in the environment variables to be more exact, I will not get into merit of discussion on this, because it does not make much sense, the variables will be accessible to any application anyway, both ways, using a array/object
or using the environment variables (Environment Variables) you can be as safe as you are insecure, if your server has some "evil application" it can steal your passwords wherever they are, it won’t matter, if your server is insecure then the problem not in your phps and yes on your server.
Now if your fear is someone getting access to .php
, use putenv
won’t solve anything.
In short, if your fear is some user via internet access password somehow use getenv
is not solution, it is impossible for the visitor to have access to this data unless you have exposed them with echo
, print
, print_r
or some very badly done debugger (it is highly recommended to turn off debuggers in the production server).
The use of
getenv
andputenv
won’t make it any safer, I don’t know who told you, but this is very relative.– Guilherme Nascimento
@Guilhermenascimento What is the location and way to assign the password of my database safely?
– Gladison
It’s not a matter of location, it’s a matter of understanding what something is "relative", I can come and tell you that plane is the safest form of transport in the world and yet there is a very serious plane accident, there are many factors to take into account, there is no magic formula ready and only to do something, Right now I’m running out of time to explain the security issue, but I’ll tell you what, between using
putenv
and aarray
with the navigation data you will not have much difference.– Guilherme Nascimento
It is easier to create a separate file with the user, password and already make the connection there, and then use a php file encryption tool like the PHP Lockit, then just include the encrypted file in include/require in your other files that require connection;
– Don't Panic