Me gustaria compartir una clase , sencilla y el modo de como la utilizo, ya que soy nuevo y pudean ayudarme a retroalimentacion.
delphi
<?php class clsUsuario{ private $IdUsuario; private $Nombre; private $Clave; private $Password; private $Activo; private $Admin; private $Existe; private $IsPassword; private $IsValido; private $user_password; public function __construct() { } function BuscaClave($usuario, $password) { /* Valida si existe la clave de Usuario */ $this->user_password = $password; $this->Clave = $usuario; if ($usuario=="admin") { $this->Existe = True; $this->Admin = "S"; $this->Password = "admin"; $this->Nombre = "Administrador"; $this->Clave = "admin"; $this->IdUsuario = 0; $this->Activo = "S"; } else { $con = new phpDataClass( DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE ); $query = "Select * From csw0010 Where Clave = '$usuario'"; $objreg = $con->recordsList( $query ); $Clave = $usuario; if ( count($objreg) == 0 ){ $this->Existe = False; } else { $this->Existe = True; if ( $obj = $con->recordsList( $query ) ) { foreach( $obj as $row ) { $this->IdUsuario = $row["IdUser"]; $this->Nombre = $row["Nombre"]; $this->Clave = $row["Clave"]; $this->Activo = $row["Activo"]; $this->Password = $row["Password"]; $this->Admin = $row["Admin"]; } } } $con->closeConnection(); } } function BuscaId($IdUsuario) { $con = new phpDataClass( DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE ); $query = "Select * From csw0010 Where IdUser = " . $IdUsuario; $objreg = $con->recordsList( $query ); if ( count($objreg) == 0 ){ $this->Existe = False; } else { $this->Existe = True; if ( $obj = $con->recordsList( $query ) ) { foreach( $obj as $row ) { $this->IdUsuario = $row["IdUser"]; $this->Nombre = $row["Nombre"]; $this->Clave = $row["Clave"]; $this->Activo = $row["Activo"]; $this->Password = $row["Password"]; $this->Admin = $row["Admin"]; } } } $con->closeConnection(); } function IsExiste(){ return $this->Existe; } function IsAdmin(){ return $this->Admin; } function IsValido() { $this->IsValido = true; if (!$this->Existe) { $this->IsValido = false; //CSW_mensaje('Usuario incorrecto.',2); } else { if (!$this->IsPassword( $this->user_password )) { $this->IsValido = false; //CSW_mensaje('Constraseña incorrecta.',2); } else { $_SESSION["id_user"] = $this->IdUsuario; $_SESSION["usuario"] = $this->Clave; $_SESSION["nombre"] = $this->Nombre; } } return $this->IsValido; } private function IsPassword($password){ if ( $this->Password == $password ){ $this->IsPassword = True; } else { $this->IsPassword = False; } return $this->IsPassword; } /* Funciones para obtener la informacion */ public function IdUsuario(){ return $this->IdUsuario; } public function Nombre(){ return $this->Nombre; } public function Clave(){ return $this->Clave; } public function Password(){ return $this->Password; } public function Activo(){ return $this->Activo; } public function Admin(){ return $this->Admin; } /* Funciones para asignar valores */ function Set_IdUsuario($SetIdUsuario){ $this->IdUsuario = $SetIdUsuario; } function Set_Nombre($SetNombre){ $this->Nombre = $SetNombre; } function Set_Clave($SetClave){ $this->Clave = $SetClave; } function Set_Password($SetPassword){ $this->Password = $SetPassword; } function Set_Activo($SetActivo){ $this->Activo = $SetActivo; } function Set_Admin($SetAdmin){ $this->Admin = $SetAdmin; } /* Funcion para grabar en la base de datos */ function Grabar(){ $Grabo = True; $Valido = True; /* Validaciones antes de Grabar */ if (strlen($this->Nombre )==0){ $Valido = False; CSW_mensaje('El Nombre es obligatorio.',2); } if (strlen($this->Clave )==0){ $Valido = False; CSW_mensaje('Clave de Usuario es obligatorio.',2); } if ($Valido) { /* Grabado de Informacion */ $con = new phpDataClass( DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE ); if ($this->IdUsuario==0){ /* Registro Nuevo */ $query = "Insert Into csw0010(Nombre,Clave,Password,Admin,Activo) ". "Values('$this->Nombre', '$this->Clave', '$this->Password', '$this->Admin', '$this->Activo') "; if ($con->executeQuery($query)) { CSW_mensaje('Registro guardado con exito',1); } else { CSW_mensaje('Error al insertar registro.',2); CSW_mensaje( $con->lastError(), 2 ); } } else { /* Modificacion de Registro */ $query = "Update csw0010 Set Nombre = '".$this->Nombre."', Clave = '".$this->Clave."', Password = '".$this->Password."', Admin = '".$this->Admin."', Activo = '".$this->Activo."' Where IdUser = ".$this->IdUsuario; if ($con->executeQuery($query)) { CSW_mensaje('Registro guardado con exito',1); } else { CSW_mensaje('Error al insertar registro.',2); CSW_mensaje( $con->lastError(), 2 ); } } // echo $query; $con->closeConnection(); } return $Grabo; } function Nuevo(){ $this->IdUsuario=0; $this->Nombre = ""; $this->Clave = ""; $this->Admin = "S"; $this->Activo = "S"; } }
y lo consumo de la siguiente manera
delphi
require_once("class/usuario.php");
delphi
$objuser = new clsUsuario; $objuser->BuscaClave( $this->EdUsuario->Text, $this->EdPassword->Text ); if ( $objuser->IsValido() ) { header ("Location: index.html"); }