Ir al contenido


Foto

Nuevo en Clases PHP


  • Por favor identifícate para responder
1 respuesta en este tema

#1 ramflores

ramflores

    Member

  • Miembros
  • PipPip
  • 42 mensajes
  • LocationMonterrey, Mexico

Escrito 31 octubre 2014 - 09:58

Buen dia Foro.

Me gustaria compartir una clase , sencilla y el modo de como la utilizo, ya que soy nuevo y pudean ayudarme a retroalimentacion.



delphi
  1. <?php
  2.  
  3. class clsUsuario{
  4.  
  5. private $IdUsuario;
  6. private $Nombre;
  7. private $Clave;
  8. private $Password;
  9. private $Activo;
  10. private $Admin;
  11.  
  12. private $Existe;
  13. private $IsPassword;
  14. private $IsValido;
  15.  
  16. private $user_password;
  17.  
  18. public function __construct() {
  19. }
  20.  
  21. function BuscaClave($usuario, $password) {
  22. /* Valida si existe la clave de Usuario */
  23. $this->user_password = $password;
  24. $this->Clave = $usuario;
  25. if ($usuario=="admin") {
  26. $this->Existe = True;
  27. $this->Admin = "S";
  28. $this->Password = "admin";
  29. $this->Nombre = "Administrador";
  30. $this->Clave = "admin";
  31. $this->IdUsuario = 0;
  32. $this->Activo = "S";
  33. } else {
  34. $con = new phpDataClass( DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE );
  35. $query = "Select * From csw0010 Where Clave = '$usuario'";
  36. $objreg = $con->recordsList( $query );
  37. $Clave = $usuario;
  38.  
  39. if ( count($objreg) == 0 ){
  40. $this->Existe = False;
  41. } else {
  42. $this->Existe = True;
  43. if ( $obj = $con->recordsList( $query ) ) {
  44. foreach( $obj as $row ) {
  45. $this->IdUsuario = $row["IdUser"];
  46. $this->Nombre = $row["Nombre"];
  47. $this->Clave = $row["Clave"];
  48. $this->Activo = $row["Activo"];
  49. $this->Password = $row["Password"];
  50. $this->Admin = $row["Admin"];
  51. }
  52. }
  53. }
  54. $con->closeConnection();
  55. }
  56. }
  57.  
  58. function BuscaId($IdUsuario) {
  59. $con = new phpDataClass( DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE );
  60. $query = "Select * From csw0010 Where IdUser = " . $IdUsuario;
  61. $objreg = $con->recordsList( $query );
  62.  
  63. if ( count($objreg) == 0 ){
  64. $this->Existe = False;
  65. } else {
  66. $this->Existe = True;
  67. if ( $obj = $con->recordsList( $query ) ) {
  68. foreach( $obj as $row ) {
  69. $this->IdUsuario = $row["IdUser"];
  70. $this->Nombre = $row["Nombre"];
  71. $this->Clave = $row["Clave"];
  72. $this->Activo = $row["Activo"];
  73. $this->Password = $row["Password"];
  74. $this->Admin = $row["Admin"];
  75. }
  76. }
  77. }
  78. $con->closeConnection();
  79. }
  80.  
  81. function IsExiste(){
  82. return $this->Existe;
  83. }
  84.  
  85. function IsAdmin(){
  86. return $this->Admin;
  87. }
  88.  
  89. function IsValido() {
  90. $this->IsValido = true;
  91. if (!$this->Existe) {
  92. $this->IsValido = false;
  93. //CSW_mensaje('Usuario incorrecto.',2);
  94. } else {
  95. if (!$this->IsPassword( $this->user_password )) {
  96. $this->IsValido = false;
  97. //CSW_mensaje('Constraseña incorrecta.',2);
  98. } else {
  99. $_SESSION["id_user"] = $this->IdUsuario;
  100. $_SESSION["usuario"] = $this->Clave;
  101. $_SESSION["nombre"] = $this->Nombre;
  102. }
  103. }
  104. return $this->IsValido;
  105. }
  106.  
  107. private function IsPassword($password){
  108. if ( $this->Password == $password ){
  109. $this->IsPassword = True;
  110. } else {
  111. $this->IsPassword = False;
  112. }
  113. return $this->IsPassword;
  114. }
  115.  
  116. /* Funciones para obtener la informacion */
  117. public function IdUsuario(){
  118. return $this->IdUsuario;
  119. }
  120. public function Nombre(){
  121. return $this->Nombre;
  122. }
  123. public function Clave(){
  124. return $this->Clave;
  125. }
  126. public function Password(){
  127. return $this->Password;
  128. }
  129. public function Activo(){
  130. return $this->Activo;
  131. }
  132. public function Admin(){
  133. return $this->Admin;
  134. }
  135.  
  136. /* Funciones para asignar valores */
  137. function Set_IdUsuario($SetIdUsuario){
  138. $this->IdUsuario = $SetIdUsuario;
  139. }
  140. function Set_Nombre($SetNombre){
  141. $this->Nombre = $SetNombre;
  142. }
  143. function Set_Clave($SetClave){
  144. $this->Clave = $SetClave;
  145. }
  146. function Set_Password($SetPassword){
  147. $this->Password = $SetPassword;
  148. }
  149. function Set_Activo($SetActivo){
  150. $this->Activo = $SetActivo;
  151. }
  152. function Set_Admin($SetAdmin){
  153. $this->Admin = $SetAdmin;
  154. }
  155.  
  156. /* Funcion para grabar en la base de datos */
  157. function Grabar(){
  158. $Grabo = True;
  159. $Valido = True;
  160.  
  161. /* Validaciones antes de Grabar */
  162. if (strlen($this->Nombre )==0){
  163. $Valido = False;
  164. CSW_mensaje('El Nombre es obligatorio.',2);
  165. }
  166. if (strlen($this->Clave )==0){
  167. $Valido = False;
  168. CSW_mensaje('Clave de Usuario es obligatorio.',2);
  169. }
  170.  
  171. if ($Valido) {
  172. /* Grabado de Informacion */
  173. $con = new phpDataClass( DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE );
  174. if ($this->IdUsuario==0){
  175. /* Registro Nuevo */
  176. $query = "Insert Into csw0010(Nombre,Clave,Password,Admin,Activo) ".
  177. "Values('$this->Nombre', '$this->Clave', '$this->Password', '$this->Admin', '$this->Activo') ";
  178. if ($con->executeQuery($query)) {
  179. CSW_mensaje('Registro guardado con exito',1);
  180. } else {
  181. CSW_mensaje('Error al insertar registro.',2);
  182. CSW_mensaje( $con->lastError(), 2 );
  183. }
  184. } else {
  185. /* Modificacion de Registro */
  186. $query = "Update csw0010
  187. Set Nombre = '".$this->Nombre."',
  188. Clave = '".$this->Clave."',
  189. Password = '".$this->Password."',
  190. Admin = '".$this->Admin."',
  191. Activo = '".$this->Activo."'
  192.   Where IdUser = ".$this->IdUsuario;
  193. if ($con->executeQuery($query)) {
  194. CSW_mensaje('Registro guardado con exito',1);
  195. } else {
  196. CSW_mensaje('Error al insertar registro.',2);
  197. CSW_mensaje( $con->lastError(), 2 );
  198. }
  199. }
  200. // echo $query;
  201. $con->closeConnection();
  202. }
  203. return $Grabo;
  204. }
  205.  
  206. function Nuevo(){
  207. $this->IdUsuario=0;
  208. $this->Nombre = "";
  209. $this->Clave = "";
  210. $this->Admin = "S";
  211. $this->Activo = "S";
  212. }
  213.  
  214. }



y lo consumo de la siguiente manera



delphi
  1.         require_once("class/usuario.php");





delphi
  1.         $objuser = new clsUsuario;
  2.         $objuser->BuscaClave( $this->EdUsuario->Text, $this->EdPassword->Text );
  3.         if ( $objuser->IsValido() ) {
  4.             header ("Location: index.html");
  5.         }


  • 0

#2 poliburro

poliburro

    Advanced Member

  • Administrador
  • 4.945 mensajes
  • LocationMéxico

Escrito 31 octubre 2014 - 10:20

Solo un consejo amigo.

Antes de concatenar tu variable a la consulta debes asegurarte que no tenga códigos de control que puedan comprometer la seguridad de tu sitio. Para ello te recomiendo una leída del siguiente artículo

http://php.net/manua...l-injection.php
  • 0




IP.Board spam blocked by CleanTalk.