1

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
  • 75
  • 1
  • 10
  • is `print_r($config);` returning empty value ? if so then that path may not be correct. – Mojo Allmighty Apr 28 '17 at 08:02
  • What is the output of `sudo service mysql status` command? – Xaqron Apr 28 '17 at 08:05
  • @Mojo Allmighty If I run my script locally, it prints values, but on the server it does not return anything. – Dosentti Apr 28 '17 at 08:13
  • This may help http://stackoverflow.com/questions/4448467/cant-connect-to-local-mysql-server-through-socket-var-lib-mysql-mysql-sock – RiggsFolly Apr 28 '17 at 08:13
  • @Xaqron, I don't have sudo configured on my on pc and I don't have access to server files deep enough to use sudo there. (or.. I don't know how. I tried through shell) – Dosentti Apr 28 '17 at 08:15
  • @Dosentti when i have troubles with my paths, i usually print out those paths content with `scandir()` . You can try this although is more like peasant way – Mojo Allmighty Apr 28 '17 at 08:15
  • @RiggsFolly Nope. I'm not trying to connect localhost – Dosentti Apr 28 '17 at 08:16
  • Without giving away the crown jewels what does `addr` look like – RiggsFolly Apr 28 '17 at 08:17
  • And if you just HardCode the values into a `new mysqli()` does it work then? – RiggsFolly Apr 28 '17 at 08:18
  • @RiggsFolly Yes it does. – Dosentti Apr 28 '17 at 08:21
  • @MojoAllmighty that scandir() doesn't print anything for me.Tried it like this: $arr = scandir('../../Takeahike'); foreach($arr as $file){ echo $file . '
    '; }
    – Dosentti Apr 28 '17 at 08:23
  • You can try using abs paths like `dirname(__FILE__);` or `$_SERVER['DOCUMENT_ROOT']`. For example `$file = $_SERVER['DOCUMENT_ROOT'] . '/path/to/file'` – Mojo Allmighty Apr 28 '17 at 08:26
  • It seems your problem is more fundamental and something is wrong with `MySQL` service. – Xaqron Apr 28 '17 at 09:13
  • 1
    I contacted school helpdesk and it turned out that problem is not with my code but the server settings. parse_ini_file() function is disabled for security reasons. But I got another function from them that does the job. – Dosentti Apr 28 '17 at 09:42

1 Answers1

0

The problem is not with code but with server settings.

parse_ini_file() -function is disabled and this can be seen by adding error_reporting( E_ALL ); to the beginning of the php script.

This problem was solved with this function by unknown user in php.net parse_ini_file() comment section:

<?php
function parse_ini_file_quotes_safe($f)
{
$r=$null;
$sec=$null;
$f=@file($f);
for ($i=0;$i<@count($f);$i++)
{
  $newsec=0;
  $w=@trim($f[$i]);
  if ($w)
  {
   if ((!$r) or ($sec))
   {
    if ((@substr($w,0,1)=="[") and (@substr($w,-1,1))=="]") {$sec=@substr($w,1,@strlen($w)-2);$newsec=1;}
   }
   if (!$newsec)
   {
    $w=@explode("=",$w);$k=@trim($w[0]);unset($w[0]); $v=@trim(@implode("=",$w));
    if ((@substr($v,0,1)=="\"") and (@substr($v,-1,1)=="\"")) {$v=@substr($v,1,@strlen($v)-2);}
    if ($sec) {$r[$sec][$k]=$v;} else {$r[$k]=$v;}
   }
  }
}
return $r;
}
?>
Dosentti
  • 75
  • 1
  • 10