Created
November 6, 2019 00:01
-
-
Save davidchc/cfd64f54b70bd2c09c40660836b75d28 to your computer and use it in GitHub Desktop.
Exemplo de uma aplicação de login e senha
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 | |
/*Carrega as definições da aplicação*/ | |
//Inicia a sessão | |
session_start(); | |
//Define as credenciais com banco de dados | |
define("DB_DSN", "mysql:host=localhost;dbname=loja"); | |
define("DB_USER", "root"); | |
define("DB_PASS", ""); | |
//Inclui os arquivos | |
require_once "fns/database-fn.php"; | |
require_once "fns/clientes-fn.php"; | |
require_once "fns/funcionarios-fn.php"; | |
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 | |
/*Conjunto de funções para trabalhar com dados dos clientes*/ | |
/* | |
* Função para Autenticar cliente | |
*/ | |
function autenticarCliente($email, $password) { | |
//faz o sql pra ver se o cliente existe | |
$sql = "SELECT id_cli FROM clientes WHERE email = :email AND password = :password LIMIT 1 "; | |
$db = getConnectionDB(); | |
$stmt = $db->prepare($sql); | |
$stmt->bindValue(":email", $email); | |
//Senha tem q ser encriptada | |
$stmt->bindValue(":password", $password); | |
if(!$stmt->execute()){ | |
return false; | |
} | |
$cliente = $pdo->fetch(PDO::FETCH_ASSOC); | |
if(!$cliente){ | |
return false; | |
} | |
//Salva o ID do cliente numa sessão | |
$_SESSION['cliente_id'] = $cliente['id_cli']; | |
return true; | |
} | |
//Verifica se cliente está logado | |
function verificaAcessoCliente(){ | |
return isset($_SESSION['cliente_id']); | |
} | |
//Desloga cliente | |
function deslogaAcessoCliente(){ | |
return unset($_SESSION['cliente_id']); | |
} |
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 | |
//Retorna a conexão do banco de dados | |
function getConnectionDB(){ | |
static $conn = null; | |
if(!$conn){ | |
$conn = new PDO(DB_DSN, DB_USER, DB_PASS); | |
} | |
return $conn; | |
} |
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 | |
/*Conjunto de funções para trabalhar com dados dos funcionarios*/ | |
/* | |
* Função para Autenticar Funcionario | |
*/ | |
function autenticarFuncionario($email, $password) { | |
//faz o sql | |
$sql = "SELECT id_func FROM funcionarios WHERE email = :email AND password = :password LIMIT 1"; | |
$db = getConnectionDB(); | |
$stmt = $db->prepare($sql); | |
$stmt->bindValue(":email", $email); | |
//Senha tem q ser encriptada | |
$stmt->bindValue(":password", $password); | |
if(!$stmt->execute()){ | |
return false; | |
} | |
$funcionario = $pdo->fetch(PDO::FETCH_ASSOC); | |
if(!$funcionario){ | |
return false; | |
} | |
//Salva o ID do funcionario numa sessão | |
$_SESSION['funcionario_id'] = $funcionario['id_func']; | |
return true; | |
} | |
//Verifica se funcionario está logado | |
function verificaAcessoFuncionario(){ | |
return isset($_SESSION['funcionario_id']); | |
} | |
//Desloga Funcionario | |
function deslogaAcessoFuncionario(){ | |
return unset($_SESSION['funcionario_id']); | |
} | |
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 | |
require_once "bootstrap.php"; | |
if($_SERVER['REQUEST_METHOD'] == 'POST'){ | |
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL); | |
$password = filter_input(INPUT_POST, '$password'); | |
//Verifica as credenciais do cliente | |
if(!autenticarCliente($email, $password)){ | |
//dados incorretos, e redireciona | |
header("location: login.php"); | |
exit; | |
} | |
//Se passar quer dizer o cliente logou,ai redireciona para area dele | |
header("location: painel-cliente.php"); | |
} |
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 | |
require_once "bootstrap.php"; | |
if($_SERVER['REQUEST_METHOD'] == 'POST'){ | |
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL); | |
$password = filter_input(INPUT_POST, '$password'); | |
//Verifica as credenciais do cliente | |
if(!autenticarFuncionario($email, $password)){ | |
//dados incorretos, e redireciona | |
header("location: login.php"); | |
exit; | |
} | |
//Se passar quer dizer o cliente logou,ai redireciona para area dele | |
header("location: painel-funcionario.php"); | |
} |
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 | |
require_once "bootstrap.php"; | |
//Verifica se ele tem permissão de acesso | |
if(!verificaAcessoCliente()) { | |
header("location: login.php"); | |
//Acesso negado | |
exit; | |
} |
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 | |
require_once "bootstrap.php"; | |
//Verifica se ele tem permissão de acesso | |
if(!verificaAcessoFuncionario()) { | |
header("location: login.php"); | |
//Acesso negado | |
exit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment