pdo :: __ construct (): php_network_getaddresses: getaddrinfo failed: name or service not known in

2

I am uploading my website to the server and when logging in I vote this error. I have a connection page in which I detail all the configuration

define('URL', 'https://www.aynii.pe/');
define('APLICATION', 'aplication/');
define('DB_TYPE', 'mysql');
define('DB_HOST', 'aynii.pe');
define('DB_USER', '');
define('DB_PASSWORD', '');
define('DB_NAME', 'aynii_db');
define('DB_CHARSET', 'utf8');
define('HASH_PASSWORD_KEY', '@nexus and artic fox Systems and Information [email protected]');
define('DEFAULT_CONTROLLER', 'login');

and I receive it in the DataBase class

public function __construct($DB_TYPE,$DB_HOST,$DB_NAME,$DB_USER,$DB_PASS,$OPCIONES)
{
    parent:: __construct($DB_TYPE.':host='.$DB_HOST.';dbname='.$DB_NAME,$DB_USER,$DB_PASS,$OPCIONES);
    parent:: setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}

I can not find the error because there are times when I get the error and others that enter normal

    
asked by Fernando Abel Gonzales Ch 18.08.2018 в 01:36
source

1 answer

1

I do not know why your class has a constructor that invokes another constructor .

If you want to implement a utilitarian class to manage your connections, you can simply make it extend% of% co_ and use the constructor of PDO to create the instance of the connection.

For security , the connection credentials should not exist defined in constants no matter where. They should be private members of the class, or, for more security, save them in a configuration file that is protected and even isolated for other users with less privileges.

Let's see an example where connection credentials exist as members of the class.

I would propose a class similar to this:

<?php

class DataBase extends PDO
{
    private $pdo;
    private $dbtype="mysql";
    private $host="localhost";  //El host suele ser este, no el nombre de dominio
    private $dbname="aynii_db";
    private $dbcharset="utf8";
    private $usr="";
    private $pwd="";

    public function __construct()
    {
        $this->Connect();

    }

    private function Connect()
    {
        $host=$this->host;
        $dsn = $this->dbtype.":host=".$this->host.";dbname=".$this->dbname.";charset=".$this->dbcharset;
        $arrOptions = array(
            PDO::ATTR_EMULATE_PREPARES => FALSE,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        );

        try 
        {
            $this->pdo = new PDO($dsn, $this->usr, $this->pwd, $arrOptions);
        }
        catch (PDOException $e) 
        {
            error_log($this->error = $e->getMessage(),0);
        }
    }
}

?>

Here we pass all the options in the constructor using PDO , that way we avoid continuing to manage the object once created. We also include the option $arrOptions in ATTR_EMULATE_PREPARES to prevent emulated preparations that can be exploited by malicious users to try to emulate prepared queries and inject malicious code.

How to use

The class would be used simply like this:

$db=new Database();

And you would already have a FALSE an instance of the connection, without having to be transferring credentials.

    
answered by 18.08.2018 / 02:36
source