1
I am trying to get my login depending on what happens in the internal part of PHP I warn if it is the email, or if the user is blocked, I am developing this all in PHP, using as little as possible javascript. However, the first time you run the site works, the second time you get the login wrong.
Follows the codes.
index php.
<!DOCTYPE html>
<?php
include 'scripts/help.php';
?>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<title>JR Tela de login</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<!--<script src="js/principal01.js"></script>-->
</head>
<body>
<div class="container">
<form class="form-signin" id="from" name="form" method="POST" action="scripts/validacao.php">
<h2 class="form-signin-heading">JR Comunicações</h2>
<div id="msg"><?php
//echo '<pre>'; print_r($_SESSION['msg']); die;
if (isset($_SESSION)) {
get_msg('msg_login');
}else{
echo "";
}
?></div>
<input type="text" name="TXT_ENDER_EMAIL" class="input-block-level" placeholder="Email">
<input type="password" name="TXT_SENHA_USUAR" class="input-block-level" placeholder="Senha">
<input class="btn btn-large btn-success" type="submit">
</form>
</div>
</body>
</html>
help php.
<?php
// Função para setar a mensagem
function set_msg($id, $msg, $tipo)
{
session_start();
if (isset($id)) {
$_SESSION['id'] = $id;
switch ($tipo) {
case 'error':
$_SESSION['msg'] = '<div class="alert-danger">' . $msg . '</div>';
break;
case 'alert':
$_SESSION['msg'] = '<div class="alert-alert">' . $msg . '</div>';
break;
default:
$_SESSION['msg'] = '<div class="alert-alert">' . $msg . '</div>';
break;
}
}
}
// Esta função vai exibir sua mensagem onde você quiser
function get_msg($id) {
//echo '<pre>'; print_r($_SESSION['msg']); die;
session_start();
if ($id == $_SESSION['id']) {
if (isset($_SESSION['msg'])) {
echo $_SESSION['msg'];
}
return FALSE;
}
}
?>
php validation.
<?php
include ("../includes/conexao.php");
include("help.php");
$email = ($_POST['TXT_ENDER_EMAIL']);
$senha = ($_POST['TXT_SENHA_USUAR']);
// Validação do usuário/senha digitados
$sql = "SELECT COD_IDENT_USUAR, TXT_NOMEX_USUAR, TXT_ENDER_EMAIL, FLG_STATU_USUAR FROM tbl_USUARIOS WHERE TXT_ENDER_EMAIL = '".$email."' AND TXT_SENHA_USUAR = '".$senha."'";
$qr = mysql_query($sql);
if (mysql_num_rows($qr) != 1) {
// Mensagem de erro quando os dados são inválidos e/ou o usuário não foi encontrado
set_msg('msg_login', 'Login ou senha inválidos', 'error');
header("Location: ../index.php"); exit; // Redireciona o visitante
} else {
// Salva os dados encontados na variável $resultado
$resultado = mysql_fetch_assoc($qr);
// Se a sessão não existir, inicia uma
if (!isset($_SESSION)) session_start();
// Salva os dados encontrados na sessão
$_SESSION['UsuarioID'] = $resultado['COD_IDENT_USUAR'];
$_SESSION['UsuarioNome'] = $resultado['TXT_NOMEX_USUAR'];
$_SESSION['UsuarioEmail'] = $resultado['TXT_ENDER_EMAIL'];
$_SESSION['UsuarioFlag'] = $resultado['FLG_STATU_USUAR'];
if($resultado['FLG_STATU_USUAR'] == 'A'){
// Redireciona o visitante
header("Location: ../paginas/principal.php"); exit;
}else{
set_msg('msg_login', 'Usuario bloqueado', 'error');
header("Location: ../index.php"); exit; // Redireciona o visitante
}
}
?>
Friend I will not answer clearly, because it seems to me that the problem is in your understanding at the time of developing, but I can give you some tips: The moment you use
session_start
the super global variable$_SESSION
is already available, soisset($_SESSION)
makes no sense.if (isset($id)) {
within the functionset_msg
also makes no sense (except for the question ofNULL
)– Guilherme Nascimento
@Guilhermenascimento is not working when I ask to give isset($_SESSION), and the first time I went around the page, but after I clicked on Ubmit, it goes and comes to me with the session and the values set.
– Renan Rodrigues
I need when he comes back from Submit he appears on the screen, I tried to do it this way, but he accuses that the session is invalid, if in case I select a value for him it is the same, only appearing a value
– Renan Rodrigues
Renan I did not give you the solution, I informed you that the use of some
isset
s and the variable$_SESSION
are wrong (no logic or need), I’m not criticizing you, I’m just telling you that it doesn’t make sense/need the way you put in the code and in my view the code itself is only a problem because of your understanding (or lack of it) about the use of super-global (http://php.net/manual/en/language.variables.superglobals.php) and functionisset
(http://php.net/manual/en/function.isset.php) and as much as I can help you fix the code, the problem goes beyond this.– Guilherme Nascimento
The more help me to find the error, I turn, I try to test more I can not..
– Renan Rodrigues
It cannot, because the code has no logic (no offense), the use of the superglobal variable called
$_SESSION
and someisset
s, I’m sorry, is not my ill will, I’m just giving you the preview of where you can find the problem, since it is not a problem necessarily of your code, but of your understanding of superglobal+isset. Perhaps some other contributor will provide a clearer answer. But the way to the solution is for you to understand theisset
and the variable$_SESSION
. I will reinforce: ... [read next comment]– Guilherme Nascimento
continuation]... isset($_SESSION) has no logic/need - if (isset($id)) { has no logic/need. One more tip, remove session_start from within the functions and put it at the top of the help.php file and move the line
<?php 
 include 'scripts/help.php';
?>
before DOCTYPE. This will probably solve part of the problem.– Guilherme Nascimento