domingo, 1 de noviembre de 2015

Cómo crear un login en PHP Orientado a objetos | How to create a login on OOP PHP

Este sistema de login consta en 7 archivos simples y fáciles de entender:

login.html

<form name="login" method="post" action="login.php">
    Username: <input name="username" class="textbox" type="text"></br>
    Password: <input name="password" class="textbox" type="password"></br>                  
    <input name="login" value="Login" class="button" type="submit"></br>      
</form>



login.php

<?php
class login
{
    public $user;
    public $post_password;
    public $query;
    public $mysql_password;
    public $error;
    public $ok;
    public $numrows;
    public $fields;

    public function __construct($username, $password)
    {   
        include "config.php";
        $this->user                 =    $username;
        $this->post_password    =    $password;
        $this->query             =    mysql_query("SELECT * FROM users WHERE username = '".$this->user."'");
        $this->mysql_password    =    mysql_fetch_array($this->query);
        $this->numrows            =    mysql_num_rows($this->query);
        $this->error            =    "Nombre de usuario o contraseña incorrectos.";
        $this->ok                =    "Bienvenido ".$this->user.". Te has logueado correctamente. <a href='menu.html'>Ir al Men&uacute; Principal</a>";
        $this->fields            =    "Por favor, rellena todos los campos.";
    }
    public function check()
    {
        if($this->user && $this->post_password)
        {
            if($this->numrows !=0)
            {
                if($this->mysql_password['password'] == md5($this->post_password))
                {
                    session_start();
                    $_SESSION['username'] = $this->user;
                    echo $this->ok;
                }
                else
                {
                    echo $this->error;
                }
            }
            else
            {
                echo $this->error;
            }
        }
        else
        {
            echo $this->fields;
        }
    }
}
include_once "clear.php";
$login = new login(clear($_POST['username']), clear($_POST['password']));
echo $login->check();
?>



register.html

<form action='register.php' method='POST'>
    Fullaname: <input type='text' name='fullname' class="textbox"></br>
    Username: <input type='text' name='username' class="textbox"></br>
    Password: <input type='password' name='password' class="textbox"></br>
    Repeat Password: <input type='password' name='repeatpassword' class="textbox"></br>
    E-Mail: <input type='text' name='email' class="textbox"></br>
    <input type='submit' name='submit' value='Register' class="button">
</form>




register.php

<?php
class register
{
    public $user;
    public $password;
    public $repassword;
    public $email;
    public $fullname;
    public $query;
    public $numrows;
    public $error;
    public $error2;
    public $error3;
    public $error4;
    public $ok;
    public $fields;
    public $regquery;
  
    public function __construct($username, $password, $repeatpassword,  $email, $fullname)
    {
    include "config.php";
        $this->user         =    $username;
        $this->password        =    $password;
        $this->repassword    =    $repeatpassword;
        $this->email        =    $email;
        $this->fullname        =    $fullname;
        $this->query        =    mysql_query("SELECT * FROM users WHERE username = '".$this->user."'");
        $this->numrows        =    mysql_num_rows($this->query);
        $this->error        =    "Nombre de usuario en uso.";
        $this->error2        =    "Las contraseñas no coinciden";
        $this->error3        =    "Nombre completo y/o nombre de usuarios superan los 25 caracteres.";
        $this->error4        =    "La contraseña debe tener un minimo de 6 caracteres y un maximo de 25";
        $this->ok            =    "Te has registrado correctamente";
        $this->fields        =    "Por favor, rellena todos los campos.";
    }
    public function check()
    {
        if($this->numrows!=0)
        {
            die ($this->error);
        }
        if($this->user&&$this->password&&$this->email&&$this->fullname)
        {
            if($this->password == $this->repassword)
            {
                if(strlen($this->username)>25||strlen($this->fullname)>25)
                {
                    echo $this->error3;
                }
                else
                {
                    if(strlen($this->password)>25||strlen($this->password)<6)
                    {
                        echo $this->error4;
                    }
                    else
                    {
                        $this->password = md5($this->password);
                        $register = mysql_query("INSERT INTO `users` (`id`, `fullname`, `username`, `password`, `email`, `admin`) VALUES (NULL, '".$this->fullname."', '".$this->user."', '".$this->password."', '".$this->email."', '0')");
                        echo $this->ok;
                    }
                }
            }
            else
            {
                echo $this->error2;
            }          
        }
        else
        {
            echo $this->fields;
        }
    }
}
include_once "clear.php";
$reg = new register(clear($_POST['username']), clear($_POST['password']), clear($_POST['repeatpassword']), clear($_POST['email']), clear($_POST['fullname']));
echo $reg->check();
?>





clear.php


<?php
function clear($texto)
{
    $limpio = strip_tags($texto);
    $limpio = htmlspecialchars($limpio);
    $limpio = stripslashes($limpio);
    return $limpio;
}
?>



config.php

<?php

$error1 = 'Problem connecting to Host';
$error2 = 'Problem connecting to MySQL';
$connect = mysql_connect ('127.0.0.1','root','root') or die($error1);
$db = mysql_select_db ('repEjec') or die($error2);

?>




menu.html


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Index</title>
</head>
<body>
    <h1>Menú Principal</h1>
</body>
</html>

1 comentario: