Ver Mensaje Individual
  #10 (permalink)  
Antiguo 06/08/2009, 21:00
Avatar de pato12
pato12
 
Fecha de Ingreso: septiembre-2007
Ubicación: Salta
Mensajes: 1.620
Antigüedad: 16 años, 7 meses
Puntos: 101
Respuesta: [APORTE] ¿ Que es y como puedo hacer un sistema de usuario ?

Bueno aqui les hice el codigo en POO, pero no lo probe xD
Código php:
Ver original
  1. <?php
  2. /********************************************************\
  3. *                     SistemaRegistro                    *
  4. **********************************************************
  5. * Autor: Pato12 de forosdelweb.com                       *
  6. * Version: 1.5 (BETA)                                    *
  7. * Web del autor: halfmusic.com                           *
  8. * Descripcion:                                           *
  9. * Sirve para registrar, logiar, revisar si un            *
  10. * usuario existe.                                        *
  11. **********************************************************
  12. **********************************************************
  13. * Este scriopt PHP es libre de usar siempre              *
  14. * cuando no borren estas lineas y respeten la            *
  15. * Licencia: GNU General Public License                   *
  16. * http://creativecommons.org/licenses/GPL/2.0/deed.es_AR *
  17. *********************************************************/
  18. class SistemaRegistro{
  19.     private $data=array(
  20.                     'conexion'=>false,
  21.                     'tabla'=>false,
  22.                     'campos'=>false,
  23.                     'errores'=>array(),
  24.                     'md5'=>false
  25.                     );
  26.     public function __construct($conexion=false,$tabla=false,$campos=false){
  27.         $error=false;
  28.         if($conexion === false){
  29.             $this->setError("Conexion no valida.");
  30.             $error=true;
  31.         }
  32.         if($tabla === false){
  33.             $this->setError("Tabla no valida.");
  34.             $error=true;
  35.         }
  36.         if($campos === false || !is_array($campos)){
  37.             $this->setError("Campos no validos.");
  38.             $error=true;
  39.         }
  40.         if($error === true)
  41.             return !$error;
  42.         $this->data['tabla']=$this->filtro($tabla);
  43.         $this->data['campos']=$campos;
  44.         $this->data['conexion']=$conexion;
  45.     }
  46.     public function login($user,$clave){
  47.         if($this->data['conexion'] === false)
  48.             return $this->data['conexion'];
  49.         $clave=$this->data['md5']?md5($this->filtro($clave)):$this->filtro($clave);
  50.         $consulta=sprintf("SELECT * FROM %s WHERE %s='%s' AND %s='%s'",$this->data['tabla'],$this->data['campos'][0],$this->filtro($user),$this->data['campos'][1],$clave);
  51.         $consultar=mysql_query($consulta,$this->data['conexion']) or die(mysql_error());
  52.         return mysql_num_rows($consultar)==1;
  53.     }
  54.     public function existeUser($user){
  55.         if($this->data['conexion'] === false)
  56.             return $this->data['conexion'];
  57.         $user=stripslashes($this->filtro($user));
  58.         $consulta=sprintf("SELECT * FROM %s WHERE %s='%s'",$this->data['tabla'],$this->data['campos'][0],$this->filtro($user));
  59.         $consultar=mysql_query($consulta,$this->data['conexion']) or die(mysql_error());
  60.         return mysql_num_rows($consultar)==1;
  61.     }
  62.     public function registrar($datos,$campos){
  63.         if($this->data['conexion'] === false)
  64.             return $this->data['conexion'];
  65.         if(!is_array($campos)||!is_array($datos))
  66.             $this->setError("Los campos o datos, no son array.");
  67.         $consulta=sprintf("INSERT INTO %s (%s) VALUES (%s)",$this->data['tabla'],implode(',',$campos),'\''.implode("','",$datos).'\'');
  68.         return mysql_query($consulta,$this->data['conexion']);
  69.     }
  70.     public function getError(){
  71.         return $this->data['errores'];
  72.     }
  73.     public function filtro($value){
  74.         return addslashes(htmlentities(trim($value)));
  75.     }
  76.     private function setError($err){
  77.         $this->data['errores'][]=$err;
  78.     }
  79. }
  80. ?>
Gracias
Salu2
__________________
Half Music - www.halfmusic.com