Last active
June 28, 2016 15:58
-
-
Save Ferrmolina/cba0ba13119c3e60a4b297b64369ab6d to your computer and use it in GitHub Desktop.
Registro y Login, simple.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('HOSTNAME', 'localhost'); | |
define('USERNAME', 'root'); | |
define('PASSWORD', ''); | |
define('DATABASE', 'mi_basededatos'); | |
$enlace = new mysqli(HOSTNAME, USERNAME, PASSWORD, DATABASE); | |
$enlace->set_charset('utf8'); | |
if (!$enlace) { | |
echo "Error al Conectar"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if($_SERVER['REQUEST_METHOD'] == "POST"){ | |
$username = $_POST['user']; | |
$passViaPost = $_POST['pass']; | |
require('db.connect.php'); | |
$sql = "SELECT id, username, password FROM tabla_usuarios WHERE username = '".$username."' LIMIT 1"; | |
$ejecutar = $enlace->query($sql); | |
$contar = $ejecutar->num_rows; | |
if ($contar <= 0) { | |
// El usuario indicado no existe en la base de datos | |
} else { | |
// El usuario existe en la base de datos. Comprobar contraseña | |
$passEnDB = ""; | |
while ($row = $ejecutar->fetch_assoc()) { | |
$passEnDB = $row['password']; | |
$hashComprobado = crypt($passViaPost, $passEnDB); | |
} | |
$esCorrecto = hash_equals($passEnDB, $hashComprobado); | |
if ($esCorrecto) { | |
// La contraseña es correcta | |
// TODO: podríamos hacer un session_start() y almacenar en $_SESSION todo lo necesario | |
} else { | |
// La contraseña es incorrecta (el hash no es identico al almacenado) | |
} | |
} | |
} else { | |
// Método no aceptado (solo aceptaremos POST) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if($_SERVER['REQUEST_METHOD'] == "POST"){ | |
$user = $_POST['username']; | |
$pass = $_POST['password']; | |
// Generar password con salt | |
$cost = 10; | |
$salt = strtr(base64_encode(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM)), '+', '.'); | |
$salt = sprintf("$2a$%02d$", $cost) . $salt; | |
$hash = crypt($pass, $salt); | |
require('dbconnect.php'); | |
$sql_check = "SELECT username FROM tabla_usuarios WHERE username = '".$user."'"; | |
$ejecutar_check = $enlace->query($sql_check); | |
$contar = $ejecutar_check->num_rows; | |
if ($contar <= 0) { | |
// El usuario no existe, ok, guardemosló en la base de datos | |
// TODO: comprobar largo de nombre, caracteres, o lo que sea necesario | |
$sql = "INSERT INTO tabla_usuarios(id, username, password) VALUES (null, '".$user."', '".$hash."')"; | |
$ejecutar = $enlace->query($sql); | |
if ($ejecutar) { | |
// Registro guardado (Usuario registrado correctamente!) | |
} else { | |
// Registro no guardado (Por alguna razón no se ejecutó correctamente la consulta, revisa tu consulta) | |
} | |
} else { | |
// El usuario ya existe en la base de datos | |
} | |
} else { | |
// Método no aceptado (solo aceptaremos POST) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment