I'm trying to create a secure mysql connection and I have created a function that creates the connection and db.ini file that contains the login credentials.
db.ini:
[database]
user = username
pass = password
db = mydb
addr = myhost
I have placed ini file in my school project folder and it's outside of public_html. I'm using this php code to get the db.ini:
function dbConnect(){
if(!isset($mysqli)){
if( file_exists("../../../db.ini")){
echo "FILE EXISTS! <br>";
}
else{
echo "the file does not exist!";
}
$config = parse_ini_file("../../../db.ini");
print_r($config);
$mysqli = new mysqli($config['addr'], $config['user'], $config['pass'], $config['$db']);
}
if ($mysqli->connect_error) {
die( "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error);
}
return $mysqli;
}
Output is this:
FILE EXISTS!
Failed to connect to MySQL: (2002) Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
So the file exists but php can't fetch its contents. How do I fix this or go around it? can I save my credentials to some other format or something?
'; } – Dosentti Apr 28 '17 at 08:23