Ver Mensaje Individual
  #1 (permalink)  
Antiguo 12/09/2013, 08:22
Strings
 
Fecha de Ingreso: septiembre-2013
Mensajes: 125
Antigüedad: 10 años, 7 meses
Puntos: 3
Sistema de login en PDO

Estoy aprendiendo PDO y he realizado este sistema de login y quiero compartirlo con vosotros. Acepto criticas.

Base de dato:
Código SQL:
Ver original
  1. CREATE TABLE IF NOT EXISTS `user` (
  2.   `id` mediumint(9) NOT NULL AUTO_INCREMENT,
  3.   `username` VARCHAR(20) NOT NULL,
  4.   `password` VARCHAR(60) NOT NULL,
  5.   `date` VARCHAR(19) NOT NULL,
  6.   `ip` VARCHAR(10) NOT NULL,
  7.   PRIMARY KEY (`id`)
  8. )

/config/index.php
Código PHP:
<?php
    $connection 
= new PDO("mysql:host=hosting;dbname=base de dato","usuario","contraseña");
    
$connection->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
?>
Index.php
Código PHP:
<?php
session_start
();
include(
'/config/index.php');

if(isset(
$_SESSION['username'])):
    echo 
'Estas logeado <a href="/logout.php">Logout</a>';
else:
    if(isset(
$_POST['login'])):
        if(empty(
$_POST['username']) || empty($_POST['password'])):
            echo 
'No dejes campos en blanco';
        elseif(
strlen($_POST['username']) > 20):
            echo 
'El usuario no puede tener mas de 20 caracteres';
        elseif(
strlen($_POST['password']) > 20):
            echo 
'La contraseña no puede tener mas de 20 caracteres';
        else:
            
$login $connection->prepare("SELECT username FROM user WHERE username = :username AND password = :password");
            
$login->bindParam(':username',$_POST['username']);
            
$login->bindParam(':password',crypt($_POST['password'], '$2a$07$rieh3693fjarjeuf38cw27fg2$'));
            
$login->execute();
            if(
$login $login->fetch(PDO::FETCH_ASSOC)):
                
$_SESSION['username'] = $_POST['username'];
                
header('Location: /');
            else:
                echo 
'Datos incorrectos';
            endif;
        endif;
    endif;
    echo 
'<form action="" method="post">
        <input name="username" placeholder="Username"><br>
        <input name="password" placeholder="Password"><br>
        <input name="login" type="submit">
    </form>
    
    <a href="/register.php">Registrate</a>'
;
endif;
?>
Register.php
Código PHP:
<?php
session_start
();
include(
'/config/index.php');

if(isset(
$_SESSION['username'])):
    
header('Location: /');
else:
    if(isset(
$_POST['register'])):
        if(empty(
$_POST['username']) || empty($_POST['password'])):
            echo 
'No dejes campos en blanco';
        elseif(
strlen($_POST['username']) > 20):
            echo 
'El usuario no puede tener mas de 20 caracteres';
        else:
            
$user $connection->prepare("SELECT username FROM user WHERE username = :username");
            
$user->bindParam(':username',$_POST['username']);
            
$user->execute();
            if(
$user->fetch(PDO::FETCH_ASSOC)):
                echo 
'El usuario ya existe';
            elseif(
strlen($_POST['password']) > 20):
                echo 
'La contraseña no puede tener mas de 20 caracteres';
            elseif(
$_POST['password'] <> $_POST['password2']):
                echo 
'Las contraseñas no coinciden';
            else:
                
$register $connection->prepare("INSERT INTO user(id,username,password,date,ip) VALUES ('',:username,:password,'".date('H:i:s d/m/Y')."','".$_SERVER['REMOTE_ADDR']."')");
                
$register->bindParam(':username',$_POST['username']);
                
$register->bindParam(':password',crypt($_POST['password'], '$2a$07$rieh3693fjarjeuf38cw27fg2$'));
                
$register->execute();
                if(
$register->rowCount() > 0):
                    
$_SESSION['username'] = $_POST['username'];
                    
header('Location: /');
                else:
                    echo 
'Ha ocurrido un error';
                endif;
            endif;
        endif;
    endif;
    echo 
'<form action="" method="post">
        <input name="username" placeholder="Username"><br>
        <input name="password" placeholder="Password"><br>
        <input name="password2" placeholder="Vuelve a ingresar la contraseña"><br>
        <input name="register" type="submit">
    </form>'
;
endif;
?>
Logout.php
Código PHP:
<?php
    session_start
();
    
session_destroy();
    
header('Location: /');
?>
Prefiero que me digais en que puedo mejorar