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?