Coder Social home page Coder Social logo

usermanager's Introduction

UserManager

Simple system for login and registration of users in PHP

Manager Class

UserManager($host_name,$db_name,$db_user,$db_password) (If the database placed in the instance does not exist, it will be created automatically, the tables are created automatically)

Methods

Manager Class:

  • createUser($username,$password) - Create a new User in the database
  • validateUser($username,$password) - Validate the user and returns true or false
  • existsUser($username) - Check if the user exists and returns true or false

Installation

drag and drop Mysql.php and UserManager.php in the folder of your project

Login Example

<?php session_start();
require 'UserManager.php';
if(isset($_SESSION['usuario'])){
    header('Location: index.php');
}
$errores = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	$usuario = filter_var(strtolower($_POST['usuario']), FILTER_SANITIZE_STRING);
	$password = $_POST['password'];
	$password = hash('sha512', $password);
	if(empty($usuario) or empty($password)){
		 $errores .= 'Porfavor rellena todo los campos';
	}else{
		$UserManager = new UserManager('localhost','practica','root','');
		$validacion = $UserManager->validateUser($usuario,$password);
		if ($validacion == true) {
			$_SESSION['usuario'] = $usuario;
			header('Location: index.php');
		} else {
			$errores .= '<li>Datos Incorrectos</li>';
		}
	}
}
require 'views/login.view.php';
?>

Register example

<?php session_start();
require 'UserManager.php';
if(isset($_SESSION['usuario'])){
    header('Location: contenido.php');
}
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    $UserManager = new UserManager('localhost','practica','root','');
    $errores = '';
    $usuario = strtolower(filter_var($_POST['usuario'],FILTER_SANITIZE_STRING));
    $password = $_POST['password'];
    $password2 = $_POST['password2'];
    if(empty($usuario) or empty($password) or empty($password2)){
        $errores .= 'Porfavor rellena todo los campos';
    }
    $existencia = $UserManager->existsUser($usuario);
    if($existencia == true){
        $errores .= 'Ya existe un usuario con tu nombre'; 
    }
    $password = hash('sha512',$password);
    $password2 = hash('sha512',$password2);
    if($password != $password2){
        $errores .= 'Las contraseñas con conciden';
    }
    if(empty($errores)){
        $crear = $UserManager->createUser($usuario,$password);
        header('Location: login.php');
    }
}
require 'views/registrate.view.php';
?>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.