How to get MySQL password for connection using getenv ()

0

I currently use the default connection form to get the data of MySql , however I know that for security the getenv() is used, however I do not know how to do it to recover the connection password with my database. data.

Here's how my MySql connection is done

<?php
$con = mysqli_connect("localhost", "root", "", "guaraparivirtual");
?>
    
asked by anonymous 03.07.2017 / 14:31

2 answers

3

It does not make sense to use putenv , if it is to do this it is better to throw everything in a array or object , something like:

config.php

  

return is recognized in include

<?php

return (object) array(
    'host' => 'localhost',
    'user' => 'root',
    'pass' => 'meupassword',
    'db'   => 'meubanco',
);

pagina.php

$config = require 'config.php';

$con = new mysqli($config->host, $config->user, $config->pass, config->banco);

The issue of security

Perhaps this story that you have heard that getenv is safer is if setting refers to setting such data out of .php , in environment variables to be more accurate, I will not enter into discussion merit about this , because it does not make much sense, the variables will be accessible to any application in any way, either way, using a array/object or using the problem is not in your phps and yes in your server.

Now if your fear is that someone gets access to .php , using putenv will not solve anything.

In short, if your fear is some user via internet accessing the 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 bad debugger (it is strongly recommended to turn off debuggers on the production server ).

    
03.07.2017 / 15:54
1

Hello, you have to do something like this.

putenv("host=localhost");
putenv("user=root");
putenv("pass= ");

$db_host=getenv("host");
$db_user=getenv("user");
$db_pass=getenv("pass");
$db_name="tese";

$con = new mysqli($db_host, $db_user, $db_pass);

I hope I have helped

    
03.07.2017 / 14:34