Archivo: config.php
Descrip: Contiene datos de usuario, clave, nombre de la base de datos y ruta del host
Código PHP:
<?php
$host = "localhost";
$user = "root";
$password = "";
$db = "administrativo";
?>
Descrip: Contiene la clase y sus metodos
Código PHP:
<?php
session_start();
date_default_timezone_set("America/Caracas");
class Conf{
private $_user;
private $_password;
private $_host;
private $_database;
static $_instance;
private function __construct(){
require("config.php");
$this->_user=$user;
$this->_password=$password;
$this->_host=$host;
$this->_database=$db;
}
private function __clone(){
}
public static function getInstance(){
if(!(self::$_instance instanceof self)){
self::$_instance = new self;
}
return self::$_instance;
}
public function getUserDB(){
$var = $this->_user;
return $var;
}
public function getHostDB(){
$var = $this->_host;
return $var;
}
public function getPassDB(){
$var = $this->_password;
return $var;
}
public function getDB(){
$var = $this->_database;
return $var;
}
}
class Db{
private $servidor;
private $usuario;
private $password;
private $database;
private $conex;
private $result;
static $_instance;
private function setConexion(){
$conf = Conf::getInstance();
$this->servidor=$conf->getHostDB();
$this->database=$conf->getDB();
$this->usuario=$conf->getUserDB();
$this->password=$conf->getPassDB();
}
private function __construct(){
$this->setConexion();
try{
$this->conex = new mysqli($this->servidor, $this->usuario, $this->password, $this->database);
}catch(Exception $e){
echo $e->getMessage();
}
}
public function ejecutar($sql){
$this->result = $this->conex->query($sql);
}
public function getTotalRegistros(){
return($this->result->num_rows);
}
public function getRegistro($campobd){
if($campobd=="*"){
return($this->result->fetch_array(MYSQLI_ASSOC));
}else{
$array = $this->result->fetch_array(MYSQLI_ASSOC);
return($array[$campobd]);
}
}
public static function getInstance(){
if(!(self::$_instance instanceof self)){
self::$_instance = new self();
}
return self::$_instance;
}
}
//Ejemplo de una función para obtener la IP del cliente y obtener la dirección de donde trabajará esa maquina
function getUbicacionCliente(){
$ip=$_SERVER['REMOTE_ADDR'];
$bd = Db::getInstance();
$bd->ejecutar("SELECT * FROM maquinas_ubicaciones where ip = '".$ip."' ");
if($bd->getTotalRegistros()>1){
echo "Direccion IP comprometida";
}else{
header("Location://".$bd->getRegistro("url"));
}
}
?>