3
I have the following password for my application: I have a PHP + Mysql application, with PDO connection, I need the application to be shared with all registered companies, but each company will have its database separately.
IMPLEMENTATION STRUCTURE
Connection:
1 - Con.class.php => Responsible for connecting to the database using PDO - Singleton
2 - Config.php => Via define(); takes the values to be passed to connection - (Host, User, DB...)
Central Database:
3 - TABLE Companies => Receives the companies that will use the application, containing the connection information - (Host, User, DB, Password)
Customer Databases (db1, bd2, db3.....):
4 - TABLE Users => Contains user information, including "ID_EMPRESA" to reference which company it belongs to in addition to login information- (user_login, user_password, user_email...)
How can I make each company log into its base it will be created when registering companies, besides not having the knowledge advanced in PHP, I couldn’t find a help on this by researching.
CODES
Con.class.php
<?php
class Conn {
private static $Host = SIS_DB_HOST;
private static $User = SIS_DB_USER;
private static $Pass = SIS_DB_PASS;
private static $Dbsa = SIS_DB_DBSA;
private static $Connect = null;
private static function Conectar() {
try {
if (self::$Connect == null):
$dsn = 'mysql:host=' . self::$Host . ';dbname=' . self::$Dbsa;
$options = [ PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8'];
self::$Connect = new PDO($dsn, self::$User, self::$Pass, $options);
self::$Connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
endif;
} catch (PDOException $e) {
PHPErro($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
die;
}
return self::$Connect;
}
public static function getConn() {
return self::Conectar();
}
private function __construct() {
}
private function __clone() {
}
private function __wakeup() {
}
}
Config.php
if ($_SERVER['HTTP_HOST'] == $urlcentral):
define('HOST', $linkBanco);
define('USER', $userBanco);
define('PASS', $senhaBanco);
define('DBSA', $nomeBanco);
else:
define('HOST_CLIENTE', $linkBancoCliente);
define('USER_CLIENTE', $userBancoCliente);
define('PASS_CLIENTE', $senhaBancoCliente);
define('DBSA_CLIENTE', $nomeBancoCliente);
endif;
Is it possible to perform these actions? When the user enters the Login and Password, check which company it belongs to, and take the database connection information and it be connected in the database of the company to which it belongs. Thank you!
I think the biggest problem there is in class structuring. It makes no sense to change the constant to change the value of class property. Why not instantiate the class with the values passed by parameters, according to the client?
– Wallace Maxters
Young man, if you have any other questions, don’t hesitate to use the comments to ask...
– Wallace Maxters