Protect config.ini file gives usare error for mysqli connection

3

Based on this question: link

I found the following way to protect the config.ini file for a connection with mysqli:

;<?php
;die(); // For further security
;/*
    [database]
    driver=mysql
    host=localhost
    port=3306
    schema=base
    username=root
    password=
;*/

I get the following error:

  

SCREAM: Error suppression ignored for   Warning: syntax error, unexpected END_OF_LINE, expecting '=' in __config.php on line 5

If I use quotes, based on the link

  

(Values null, no and false results in "", yes and true results in "1".)

;<?php
    ;die(); // For further security
    ;/*
        [database]
        driver="mysql"
        host="localhost"
        port="3306"
        schema="base"
        username="root"
        password=""
    ;*/

I get the following error:

  

Warning: syntax error, unexpected END_OF_LINE, expecting '=' in __config.php on line 5

Changed to:

;<?php
;die(); // For further security
;/*[database]
host="localhost"
username="root"
password="" 
schema="base";*/

It no longer presents an error, but I think it does not parry well

configuring database:

$file ='__config.ini.php';

$config=parse_ini_file($file);

  $host = $config['database']['host'];
  echo $host;
  $user = $config['database']['user'];
  echo $user;
  $pass = $config['database']['pass'];
  echo $pass;
  $schema = $config['database']['schema'];
  echo $schema;
  

I get an error, Undefined index: database for each line

Last modification of the ini:

;<?php
;die(); // For further security
;/*
[database]
driver="mysql"
host="localhost"
port="3306"
schema="base"
username="root"
password="" 
;*/

Conexion.php

<?php

$file ='__config.ini.php';

$config=parse_ini_file($file, true);

$host=$config['database']['host'];
$user=$config['database']['username'];
$pass=$config['database']['password'];
$schema=$config['database']['schema'];

class DBConnector {
    private static $instance ;
    public function __construct($host, $user, $password, $db){
      if (self::$instance){
        exit("Instance on DBConnection already exists.") ;
      }
    }

    public static function conectar(){
      if (!self::$instance){
        self::$instance = new DBConnector(a,b,c,d) ;
      }
      return $instance ;
    }
}

$mysqli = new DBConnector($host,$user,$pass,$schema);
?>

Line to connect:

require_once '__conexion.php';
$conexion = new DBConnector($host,$user,$pass,$schema);

Error:

  

Call to undefined method DBConnector :: prepare () line 15

line 15:

$statement = $conexion->prepare($sql);

Reading on php.net

I got this:

class conexion extends mysqli {
    public function __construct($host, $usuario, $contraseña, $bd) {
        parent::__construct($host, $usuario, $contraseña, $bd);

        if (mysqli_connect_error()) {
            die('Error de Conexión (' . mysqli_connect_errno() . ') '
                    . mysqli_connect_error());
        }
    }
}

$conexion = new conexion($host,$user,$pass,$schema);

Is it safer?

    
asked by Victor Alvarado 07.03.2017 в 13:39
source

1 answer

2

Defines the .ini file with no spaces to the left:

;<?php
;die(); // For further security
;/*
[database]
driver="mysql"
host="localhost"
port="3306"
schema="base"
username="root"
password="" 
;*/

Using the $config = parse_ini_file($file); function you can access the values in this way:

<?php 
$config = parse_ini_file($file);
echo $config['driver'];
echo $config['host'];

If you want the sections to be processed, use it in this way $config = parse_ini_file($file, true); and you can access the values using a multidimensional array:

<?php 
$config = parse_ini_file($file, true);
echo $config['database']['driver'];
echo $config['database']['host'];
    
answered by 07.03.2017 / 14:12
source