I've been learning php though (I do not know if it's customary for a beginner) the code is getting a mess as I'm trying new things. I took some lessons on YouTube channel Celke however despite being well didactic some of the times it seems to me some unnecessary things when compared with other codes .. And in the code below I'm having trouble organizing myself, could anyone help me explain it?
The following code validates the login valida.php
<?php
session_start();
//Incluindo a conexão com banco de dados
include_once("conexao.php");
//O campo usuário e senha preenchido entra no if para validar
if((isset($_POST['email'])) && (isset($_POST['senha']))){
$usuario = mysqli_real_escape_string($conn, $_POST['email']); //Escapar de caracteres especiais, como aspas, prevenindo SQL injection
$senha = mysqli_real_escape_string($conn, $_POST['senha']);
$senha = md5($senha);
//Buscar na tabela usuario o usuário que corresponde com os dados digitado no formulário
$result_usuario = "SELECT * FROM usuarios WHERE email = '$usuario' && senha = '$senha' LIMIT 1";
$resultado_usuario = mysqli_query($conn, $result_usuario);
$resultado = mysqli_fetch_assoc($resultado_usuario);
//Encontrado um usuario na tabela usuário com os mesmos dados digitado no formulário
if(isset($resultado)){
$_SESSION['usuarioId'] = $resultado['id'];
$_SESSION['usuarioNome'] = $resultado['nome'];
$_SESSION['usuarioNiveisAcessoId'] = $resultado['niveis_acesso_id'];
$_SESSION['usuarioEmail'] = $resultado['email'];
$_SESSION['usuarioEndereco'] = $resultado['endereco'];
if($_SESSION['usuarioNiveisAcessoId'] == "1"){
header("Location: administrativo.php");
}elseif($_SESSION['usuarioNiveisAcessoId'] == "2"){
header("Location: colaborador.php");
}else{
header("Location: cliente.php");
}
//Não foi encontrado um usuario na tabela usuário com os mesmos dados digitado no formulário
//redireciona o usuario para a página de login
}else{
//Váriavel global recebendo a mensagem de erro
$_SESSION['loginErro'] = "Usuário ou senha Inválido";
header("Location: index.php");
}
//O campo usuário e senha não preenchido entra no else e redireciona o usuário para a página de login
}else{
$_SESSION['loginErro'] = "Usuário ou senha inválido";
header("Location: index.php");
}?>
And the user page client.php
<?php
include_once("conexao.php");
session_start();
if (isset($_SESSION['usuarioId'])) {
$usuarioid = $_SESSION['usuarioId'];
$nome_perfil = $_SESSION['usuarioNome'];
}
?>
I do not know if it's asking too much however someone could hack this code valid.php and help me with the client.php page so that I can enter db information in it, I want to dispose of the registrations carried out by the users in the however I do not know how to get the user id along with session_start. * I think the explanation of my problem was as confusing as the same.