differentiate type of user with php [closed]

0

I have a system for registering my system that has two types of users: teacher and student. has a field in the form that is the "user type" of which can be teacher or student. I made a code, but it is very ugly and wanted to help me. It works, but I wanted to know if it has how to do it in a different and simpler way. If you are a student you have to send one page and the teacher sends it to another. follow the code:

<?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

    session_start();
    include_once "conecta.php";

    if(isset($_POST)){
        $email = $_POST['email'];
        $senha = $_POST['senha'];

        if(isset($conexao)){
            $stmt = mysqli_prepare($conexao,"select email from aluno where email = ? and senha = ?");
            mysqli_stmt_bind_param($stmt, "ss", $email, $senha);

            mysqli_stmt_execute($stmt);

            mysqli_stmt_bind_result($stmt, $resultado);
            mysqli_stmt_fetch($stmt);

            if(isset($resultado) > 0){
                $_SESSION['login'] = $email;
                $_SESSION['tipousuario'] = "aluno";
                header("Location: control/home.php");
            }else{
                echo "Usuário ou senha incorretos";
                header("Location: index.php");
            }
        }

        if(isset($conexao)){
            $stmt = mysqli_prepare($conexao, "select email from professor where email = ? and senha = ?");
            mysqli_stmt_bind_param($stmt, "ss", $email, $senha);

            mysqli_execute($stmt);

            mysqli_stmt_bind_result($stmt, $resultado);
            mysqli_stmt_fetch($stmt);

            if(isset($resultado) > 0){
                $_SESSION['login'] = $email;
                $_SESSION['tipousuario'] = "professor";
                header("Location: control/home2.php");
            }else{
                echo "Usuário ou senha incorretos";
                header("Location: index.php");
            }
        }
    }
?>
    
asked by anonymous 06.06.2017 / 01:01

3 answers

1

I believe that instead of echo "Usuário ou senha incorretos"; you could pass the access error message to the index by parameter, to display to the user. I would also try extracting a few methods / isolating things a bit in php functions to leave yours more readable code and facilitate maintenance, for example, isolating teacher and student verification (not tested):

<?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

    session_start();
    include_once "conecta.php";

    if(isset($_POST)){
        $email = $_POST['email'];
        $senha = $_POST['senha'];
        $_SESSION['login'] = $email;

        if(isAluno($conexao,$email,$senha)){

            $_SESSION['tipousuario'] = "aluno";
            header("Location: control/home.php");

        }else if(isProfessor($conexao,$email,$senha)){

            $_SESSION['tipousuario'] = "professor";
            header("Location: control/home2.php");

        }else{
            header("Location: index.php?msg=Usuário ou senha incorretos");
        }


    }

        /**
     Função para verificar se é professor
     */
    function isProfessor($conexao,$email, $senha){
        $retorno=false;
        if(isset($conexao)){
            $stmt = mysqli_prepare($conexao,"select email from professor where email = ? and senha = ?");
            mysqli_stmt_bind_param($stmt, "ss", $email, $senha);
            mysqli_stmt_execute($stmt);
            mysqli_stmt_bind_result($stmt, $resultado);
            mysqli_stmt_fetch($stmt);

            if(isset($resultado) > 0){$retorno=true;}
        }
        return $retorno;
    }
    /**
     Função para verificar se é aluno
     */
    function isAluno($conexao,$email, $senha){
        $retorno=false;
        if(isset($conexao)){
            $stmt = mysqli_prepare($conexao,"select email from aluno where email = ? and senha = ?");
            mysqli_stmt_bind_param($stmt, "ss", $email, $senha);
            mysqli_stmt_execute($stmt);
            mysqli_stmt_bind_result($stmt, $resultado);
            mysqli_stmt_fetch($stmt);

            if(isset($resultado) > 0){$retorno=true;}
        }
            return $retorno;
    }
?>

Also, if there are many functions in this file, you may prefer to create a CLASS php that contains these functions to be reused.

    
06.06.2017 / 14:53
1

It does not have much to simplify, but I've reorganized it into your code

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

session_start();
include_once "conecta.php";

if(isset($_POST)){
    $email = $_POST['email'];
    $senha = $_POST['senha'];

    if(isset($conexao)){
        $stmt = mysqli_prepare($conexao,"select email from aluno where email = ? and senha = ?");
        mysqli_stmt_bind_param($stmt, "ss", $email, $senha);

        mysqli_stmt_execute($stmt);

        mysqli_stmt_bind_result($stmt, $resultado);
        mysqli_stmt_fetch($stmt);

        if(isset($resultado) > 0){
            $_SESSION['login'] = $email;
            $_SESSION['tipousuario'] = "aluno";
            header("Location: control/home.php");
         }else{
            $stmt = mysqli_prepare($conexao, "select email from professor where email = ? and senha = ?");
            mysqli_stmt_bind_param($stmt, "ss", $email, $senha);

            mysqli_execute($stmt);

            mysqli_stmt_bind_result($stmt, $resultado);
            mysqli_stmt_fetch($stmt);

            if(isset($resultado) > 0){
               $_SESSION['login'] = $email;
               $_SESSION['tipousuario'] = "professor";
               header("Location: control/home2.php");
            }else{
               echo "Usuário ou senha incorretos";
               header("Location: index.php");
            }           

        }
    }
}
    
06.06.2017 / 02:47
0

Actually you would solve this in the first If! If $ _SESSION ['usertype'] is different from 'student', run the select in the teacher table and direct to the teacher's page otherwise direct to the student's!

    
06.06.2017 / 13:59