Ver Mensaje Individual
  #5 (permalink)  
Antiguo 05/08/2010, 22:36
Avatar de CCB
CCB
 
Fecha de Ingreso: noviembre-2009
Ubicación: Perú
Mensajes: 65
Antigüedad: 16 años, 11 meses
Puntos: 3
Respuesta: Registro de usuarios con class php

pues a ver si con esto esta mejor, a mi me resulta incluso me pase probando la seguridad, pero como recin empiezo en class siempre se nos escapa algo.

Luego aporto el codigo completo pero esto funciona solo con ese config.php anterior que pase.

Código PHP:
<?php
include("config.php");

    class 
registro_usuarios
    
{
        var 
$user;
        var 
$pass;
        var 
$email;
        var 
$name;

         public function 
tratandovariables()
        {

            
/* Eliminando Caracteres Especiales */
            
$usuario = htmlspecialchars(strip_tags($_POST['usuario']));
            
$password = htmlspecialchars($_POST['pass']);
            
$mail = htmlspecialchars(strip_tags($_POST['mail']));
            
$nombre = htmlspecialchars(strip_tags($_POST['nombre']));

            if (
preg_match("/^[a-zA-Z0-9\-_]{3,20}$/", $usuario))
            {
                
/* Asignando Valor */
                
$this->user = $usuario;
                
$this->pass = md5($password);
                
$this->email = $mail;
                
$this->name = $nombre;
            }
            else
            {
                echo 
"El nombre de usuario no es válido<br>";
                exit;
            }
        }

        public function 
register()
        {
            
$this->tratandovariables();

            
/* Conectando la Base de Datos */
            
conexion();

            
/* Comprobando si existe el usuario */
            
$check = sprintf("select user from usuarios where user = '%s'",  $this->user);
            
$qry = mysql_query($check);

            
/* La compracion */
                
if (mysql_num_rows($qry))
                {
                    echo 
"Lo sentimos, el nombre de usuario ya esta registrado.<br />";
                    
mysql_free_result($qry);
                    exit;
                } else
                {
                    
$insert = sprintf("insert into usuarios (id_cliente, user, pass, nom, email) values (NULL, '%s', '%s', '%s', '%s')",  $this->user, $this->pass, $this->name, $this->email);
                    
$qry = mysql_query($insert);
                        if(
mysql_affected_rows())
                        {
                            echo 
"El Usuario $this->user se Registro Correctamente";
                        }
                        else
                        {
                            echo 
"Error Ingresando datos";
                        }
                    exit;
                }
        }

    }

    
$registrando = new registro_usuarios();
    
$registrando->register();

?>


Nota: Corregi ereg por preg_match

Última edición por CCB; 08/08/2010 a las 00:20 Razón: Corrigiendo.