When start the site already ask login

Asked

Viewed 207 times

4

I’m creating a website and I’m having a question. How do I stop when the user enters the URL address or clicks a button that goes to admin.html, and automatically appears the login page? that is how to start admin.html already appear first the login page so that later will open admin.html

Of course if I think about doing it by the button I put as href. But if when the úsuario type the url address, then the login page will not appear. how could do?

my following files:

login.php php connection. Adminagenda.html

login.php:

<?php 
include("php/conexao.php");

if(isset($_POST['email']) && strlen($_POST['email']) > 0){

    if(!isset($_SESSION))
        session_start();

    $_SESSION['email'] = $mysqli -> escape_string($_POST['email']);
    $_SESSION['senha'] = $_POST['senha'];


    $sql_code = "SELECT senha, codigo FROM usuario WHERE email = '$_SESSION[email]'";
    $sql_query = $mysqli -> query($sql_code) or die ($mysqli -> error);
    $dado = $sql_query->fetch_assoc();
    $total = $sql_query-> num_rows;


    if($total == 0){
        $erro[] = "Este email não pertence a nenhum usuário.";
    }
    else{
        if($dado['senha'] == $_SESSION['senha']){

            $_SESSION['usuario'] = $dado['codigo'];

        } else{

            $erro[] = "Senha incorreta.";
        }

    }

    if(count($erro) == 0 || !isset($erro)){
        echo "<script>alert('Login efetuado com sucesso... Seja bem vindo'); location.href='AdmAgenda.html';</script>";
    }

}


?>



<html>
<head></head>
<body>
<?php if(count($erro) > 0)
        foreach($erro as $msg){
            echo "<p>$msg</p>";
        }


    ?>

    <form method="POST" action="">
    <input value="" type="text" placeholder="email" name="email">
    <input type="password" name="senha">

        <input type="submit" value="Entrar">



    </form>


    </body>

</html>

Adminagenda.html:

<!DOCTYPE html>
<html lang="pt-br" ng-app="AngularADM">

<head>
    <meta charset="utf-8">
    <meta name="author" content="Miyomic">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Administração</title>



    <link rel="stylesheet" type="text/css" href="css/visual.css">

    <!-- responsivo -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/bootstrap.css" rel="stylesheet">
    <link href="css/bootstrap-theme.css" rel="stylesheet">
    <link href="css/bootstrap-theme.css.map" rel="stylesheet">
    <link href="css/bootstrap-theme.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
    <script src="js/jqueryAtualizado.js"></script>
    <script src="js/jqueryAtualizado.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <script src="js/bootstrap.min.js"></script>
    <!-- Fim responsivo -->

    <script src="js/digitar-home.js"></script>
    <script src="js/angular.min.js"></script>
    <script src="js/teste.js"></script>
</head>

<body ng-controller="TempoHorasP1">

    <section>
    <div class="container">
        <div class="row">
          <h1 class="rosa texto-centro cabeca-adm">Beleza e Harmônia</h1>
        </div>

        <div class="alert alert-warning" role="alert">
            <p class="texto-centro">As configurações selecionadas abaixo sera alterada no Agendamento online. Por favor
        tomar cuidado nas escolhas das opções seguinte, suas definições são online.<br/> Em caso de congestionamento no servidor aguardar de 5 a 10 minutos.</p>

        </div>

        </div>

    </section>



    <div class="container">
        <div class="row">
            <h2 class="rosa texto-centro margin-top30 bold">Configuração da agenda online</h2>
            <p class="rosa texto-centro margin-top20">Selecionar as horas que estão disponiveis em seu salão</p>
        </div>

        <form method="post" action="agendamento.php">
            <div class="row texto-centro">
                <div class="col-lg-12 texto-centro caixa-adm-online">

                    <div class="hora-20">
                    <span class="letra18">10:00</span>
                    <input type="checkbox"  id="blankRadio1" value="10:00" name="hora[]" aria-label="..." ng-model="um">
                    <br />
                    </div>

                    <hr class="linha-cinza" />

                    <span class="letra18">10:30</span>
                    <input type="checkbox"  id="blankRadio1" value="10:30"  name="hora[]" aria-label="..." ng-model="dois">
                    <br />
                    <hr class="linha-cinza" />

                    <span class="letra18">11:00</span>
                    <input type="checkbox"  id="blankRadio1" value="11:00" name="hora[]" aria-label="..." ng-model="tres">
                    <br />
                    <hr class="linha-cinza" />

                    <span class="letra18">11:30</span>
                    <input type="checkbox"  id="blankRadio1" value="11:30" name="hora[]" aria-label="..." ng-model="quatro">
                    <br />
                    <hr class="linha-cinza" />

                    <span class="letra18">13:00</span>
                    <input type="checkbox" name="hora[]" id="blankRadio1" value="13:00" aria-label="..." ng-model="cinco">
                    <br />
                    <hr class="linha-cinza" />

                    <span class="letra18">13:30</span>
                    <input type="checkbox" name="hora[]" id="blankRadio1" value="13:30" aria-label="..." ng-model="seis">
                    <br />
                    <hr class="linha-cinza" />

                    <span class="letra18">14:00</span>
                    <input type="checkbox" name="hora[]" id="blankRadio1" value="14:00" aria-label="..." ng-model="sete">
                    <br />
                    <hr class="linha-cinza" />

                    <span class="letra18">14:30</span>
                    <input type="checkbox" name="hora[]" id="blankRadio1" value="14:30" aria-label="..." ng-model="oito">
                    <br />
                    <hr class="linha-cinza" />

                    <span class="letra18">15:00</span>
                    <input type="checkbox" name="hora[]" id="blankRadio1" value="15:00" aria-label="..." ng-model="dez">
                    <br />
                    <hr class="linha-cinza" />

                    <span class="letra18">15:30</span>
                    <input type="checkbox" name="hora[]" id="blankRadio1" value="15:30" aria-label="..." ng-model="onze">
                    <br />
                    <hr class="linha-cinza" />

                    <span class="letra18">16:00</span>
                    <input type="checkbox" name="hora[]" id="blankRadio1" value="16:00" aria-label="..." ng-model="doze">
                    <br />
                    <hr class="linha-cinza" />

                    <span class="letra18">16:30</span>
                    <input type="checkbox" name="hora[]" id="blankRadio1" value="16:30" aria-label="..." ng-model="treze">
                    <br />
                    <hr class="linha-cinza" />


                    <div class="hora-20">
                    <span class="letra18">17:00</span>
                    <input type="checkbox" name="hora[]" id="blankRadio1" value="17:00" aria-label="..." ng-model="quatorze">
                    <br />
                        </div>

                </div>
            </div>

            <div class="row texto-centro margin-top20">
                <button ng-click="atualizar()" value="descobrir" class="botao-atualizar">Atualizar <span class="glyphicon glyphicon-send branco"></span></button>

            </div>
        </form>
    </div>



</body>

</html>

I think it should be by javascript or am I mistaken? could someone help me? Thank you...

  • When the user is logged in he enters the admin and when he is not he is redirected to the login is this?

2 answers

4


Taking the idea of friend Localhost I would include/require a php file that validates whether the user is logged in or not on your system, because already thinking about the long term and the growth of your system.

This is valid because if your system grows Oce will always call the same validation file and if you need to change something maintenance will be faster and simpler than changing the validation files by files.

In your index.php would look like this:

<?php require_once 'valida.php';?>// AQUI chama o arquivo de validacao

<!DOCTYPE html>
    <html lang="pt-br" ng-app="AngularADM">

    <head>

// Restante do codigo HTML

And in your validation file, which we call here valida.php you put the validation script:

<?php 
 //verifica se o usuário não esta logado
 if(!isset($_SESSION['usuario']) || empty($_SESSION['usuario']){
     header('location:login.php');
  }else{
     header('location:AdmimAgenda.php');
  }
 ?>

This way you can change the form of validation whenever you need and quickly, changing only one file, as well as you can direct the user to the page you want if you already have a valid session.

Now on each page that needs to be validated, just do the include/require of the valida.php file

  • Only that I have a problem... when I log in it shows me the message that has successfully logged in but loads the page and back to the login page, what would that be?

  • see if Voce is recording the session at the time of login, or your file will call the file valida.php and not find the session, then it will return again to the login page

  • he shows me this mistake Undefined variable: erro in C:\nome_pasta\login.php on line 34 and on that line 34 is that line of code if(count($erro) == 0 || !isset($erro)){&#xA; echo "<script>alert('Seja bem vindo!'); location.href='admagenda.php';</script>";&#xA; } I checked and had given the wrong name, I fixed but still of that error...

  • change the condition of your if, first put the isset in the check then Count

  • changed nothing... I put in the location of the direction the link from this source and it worked now when I put admagenda.php it goes back to the login page... I believe it is in a kind of loop...

  • Dude, update your script the right way you’re using it. This loop is very likely to be happening because your validation file is trying to validate the session and it should not be set

  • Don’t forget session_start() to validate sessions

  • Okay... thank you...

Show 3 more comments

3

Well, if I understand correctly, you will have to put in the first lines of the code, which needs the login, the instruction requiring the login, and for that it needs to be .php.

So the first step is to save your AdmimAgenda.html as AdmimAgenda.php. And then insert just before the header of your page to check if the user is logged in. Then Admimagenda.php will look something like this:

<?php 
 //verifica se o usuário esta não logado
 if(!isset($_SESSION['usuario']) || empty($_SESSION['usuario']){
 // caso ele não estiver logado, exibe o link para redirecionar para o login
    echo "<a href='login.php'> Faça o login para continuar</a>";
 // e cancela o carregamento do resto da pagina...
  return;
  }
 ?>
<!DOCTYPE html>
<html lang="pt-br" ng-app="AngularADM">

<head>
    <meta charset="utf-8">
    <meta name="author" content="Miyomic">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Administração</title>



    <link rel="stylesheet" type="text/css" href="css/visual.css">

    <!-- responsivo -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/bootstrap.css" rel="stylesheet">
    <link href="css/bootstrap-theme.css" rel="stylesheet">
    <link href="css/bootstrap-theme.css.map" rel="stylesheet">
    <link href="css/bootstrap-theme.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
    <script src="js/jqueryAtualizado.js"></script>
    <script src="js/jqueryAtualizado.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <script src="js/bootstrap.min.js"></script>
    <!-- Fim responsivo -->

    <script src="js/digitar-home.js"></script>
    <script src="js/angular.min.js"></script>
    <script src="js/teste.js"></script>
</head>

<body ng-controller="TempoHorasP1">

    <section>
    <div class="container">
        <div class="row">
          <h1 class="rosa texto-centro cabeca-adm">Beleza e Harmônia</h1>
        </div>

        <div class="alert alert-warning" role="alert">
            <p class="texto-centro">As configurações selecionadas abaixo sera alterada no Agendamento online. Por favor
        tomar cuidado nas escolhas das opções seguinte, suas definições são online.<br/> Em caso de congestionamento no servidor aguardar de 5 a 10 minutos.</p>

        </div>

        </div>

    </section>



    <div class="container">
        <div class="row">
            <h2 class="rosa texto-centro margin-top30 bold">Configuração da agenda online</h2>
            <p class="rosa texto-centro margin-top20">Selecionar as horas que estão disponiveis em seu salão</p>
        </div>

        <form method="post" action="agendamento.php">
            <div class="row texto-centro">
                <div class="col-lg-12 texto-centro caixa-adm-online">

                    <div class="hora-20">
                    <span class="letra18">10:00</span>
                    <input type="checkbox"  id="blankRadio1" value="10:00" name="hora[]" aria-label="..." ng-model="um">
                    <br />
                    </div>

                    <hr class="linha-cinza" />

                    <span class="letra18">10:30</span>
                    <input type="checkbox"  id="blankRadio1" value="10:30"  name="hora[]" aria-label="..." ng-model="dois">
                    <br />
                    <hr class="linha-cinza" />

                    <span class="letra18">11:00</span>
                    <input type="checkbox"  id="blankRadio1" value="11:00" name="hora[]" aria-label="..." ng-model="tres">
                    <br />
                    <hr class="linha-cinza" />

                    <span class="letra18">11:30</span>
                    <input type="checkbox"  id="blankRadio1" value="11:30" name="hora[]" aria-label="..." ng-model="quatro">
                    <br />
                    <hr class="linha-cinza" />

                    <span class="letra18">13:00</span>
                    <input type="checkbox" name="hora[]" id="blankRadio1" value="13:00" aria-label="..." ng-model="cinco">
                    <br />
                    <hr class="linha-cinza" />

                    <span class="letra18">13:30</span>
                    <input type="checkbox" name="hora[]" id="blankRadio1" value="13:30" aria-label="..." ng-model="seis">
                    <br />
                    <hr class="linha-cinza" />

                    <span class="letra18">14:00</span>
                    <input type="checkbox" name="hora[]" id="blankRadio1" value="14:00" aria-label="..." ng-model="sete">
                    <br />
                    <hr class="linha-cinza" />

                    <span class="letra18">14:30</span>
                    <input type="checkbox" name="hora[]" id="blankRadio1" value="14:30" aria-label="..." ng-model="oito">
                    <br />
                    <hr class="linha-cinza" />

                    <span class="letra18">15:00</span>
                    <input type="checkbox" name="hora[]" id="blankRadio1" value="15:00" aria-label="..." ng-model="dez">
                    <br />
                    <hr class="linha-cinza" />

                    <span class="letra18">15:30</span>
                    <input type="checkbox" name="hora[]" id="blankRadio1" value="15:30" aria-label="..." ng-model="onze">
                    <br />
                    <hr class="linha-cinza" />

                    <span class="letra18">16:00</span>
                    <input type="checkbox" name="hora[]" id="blankRadio1" value="16:00" aria-label="..." ng-model="doze">
                    <br />
                    <hr class="linha-cinza" />

                    <span class="letra18">16:30</span>
                    <input type="checkbox" name="hora[]" id="blankRadio1" value="16:30" aria-label="..." ng-model="treze">
                    <br />
                    <hr class="linha-cinza" />


                    <div class="hora-20">
                    <span class="letra18">17:00</span>
                    <input type="checkbox" name="hora[]" id="blankRadio1" value="17:00" aria-label="..." ng-model="quatorze">
                    <br />
                        </div>

                </div>
            </div>

            <div class="row texto-centro margin-top20">
                <button ng-click="atualizar()" value="descobrir" class="botao-atualizar">Atualizar <span class="glyphicon glyphicon-send branco"></span></button>

            </div>
        </form>
    </div>



</body>

</html>

I hope I helped, if it’s not what you want comment there!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.