Last active
April 7, 2020 05:44
-
-
Save aaferrari/54073c7a1ba487d4ec43fcdaa888b1fa to your computer and use it in GitHub Desktop.
Demostración de un sistema simple para dar de alta y comprobar usuarios.
This file contains 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 | |
/* | |
* Para que funcione es necesario colocar este archivo en /var/www/localhost/htdocs/ | |
* dentro de una carpeta con permisos de escritura. Utiliza SQLite como base de datos. | |
*/ | |
header('Content-type: application/json'); | |
$user = (empty($_POST['user']) === false ? $_POST['user'] : ""); | |
$pass = (empty($_POST['pass']) === false ? $_POST['pass'] : ""); | |
$output = Array(); | |
// Por defecto se crean la base de datos y la tabla si no existen | |
$db = new SQLite3('usuarios.db'); | |
$db->exec('CREATE TABLE IF NOT EXISTS usuarios ("nombre" VARCHAR PRIMARY KEY NOT NULL UNIQUE, "pass" TEXT NOT NULL)'); | |
if (!empty($user)) { | |
// Verificamos si el usuario existe | |
$existing = $db->querySingle("select nombre from usuarios where nombre='$user'"); | |
if(empty($existing)) { | |
// Si hay una contraseña y el usuario no existe entonces se crea | |
if (!empty($pass)) { | |
$db->exec("insert into usuarios values ('$user', '" . sha1($pass) . "')"); | |
$output["created"] = true; | |
} | |
else { $output["exist"] = false; } | |
} | |
else { | |
$output["exist"] = true; | |
} | |
} | |
echo json_encode($output); | |
?> |
This file contains 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
# -*- coding: utf-8 -*- | |
import requests, json | |
url = 'http://localhost/test/usercheck.php' | |
user = input("Ingrese nombre del usuario: ") | |
passw = input("Ingrese contraseña (dejar en blanco para comprobar el nombre): ") | |
r = requests.post(url, data={"user": user, "pass": passw}) | |
response = json.loads(r.text) | |
if "exist" in response.keys(): | |
if response["exist"] == True: print("El usuario %s ya esta registrado." % user) | |
elif response["exist"] == False: print("El usuario %s no existe." % user) | |
if "created" in response.keys(): | |
if response["created"] == True: print("El usuario %s ya sido registrado exitosamente." % user) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment