PHP does not think Session

Asked

Viewed 26 times

0

Hello! I am logging in with Jquery and PHP, the connection is right. But when I soon instead of staying on a page he doesn’t find my session and sends me back to login.

My HTML:

<h2 id="errolog">Usuário ou senha errados!</h2>
    
<form id="formlogin">
	<input type="email" id="email" placeholder="Digite seu e-mail" required="" />
	<input type="password" id="senha" placeholder="Senha" required="" />
	<button type="submit">Entrar</button>
</form>

<script type="text/javascript" src="jquery-3.2.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#errolog').hide(); //Esconde o elemento com id errolog
$('#formlogin').submit(function(){ 	//Ao submeter formulário
var login=$('#email').val();	//Pega valor do campo email
var senha=$('#senha').val();	//Pega valor do campo senha
$.ajax({			//Função AJAX
  url:"manifesta_login22.php",			//Arquivo php
  type:"post",				//Método de envio
  data: "login="+login+"&senha="+senha,	//Dados
    success: function (result){			//Sucesso no AJAX
                if(result==1){
                  location.href='restrito.php'	//Redireciona
                }else{
                  $('#errolog').show();		//Informa o erro
                }
            }
})
return false;	//Evita que a página seja atualizada
})
})
</script>

Connection to PHP:

<?php
  $con = mysqli_connect("localhost","root","root","login2");

  $login=$_POST['login'];	//Pegando dados passados por AJAX
  $senha=$_POST['senha'];

  //Consulta no banco de dados
  $sql="select * from usuarios where email='".$login."' and senha='".md5($senha)."'";
  $resultados = mysqli_query($con, $sql) or die (mysqli_error());
  $res=mysqli_fetch_array($resultados); //
	if (@mysqli_num_rows($resultados) == 0) {
		echo 0;	//Se a consulta não retornar nada é porque as credenciais estão erradas
	}

	else {
		echo 1;	//Responde sucesso
		if(!isset($_SESSION)) {	//verifica se há sessão aberta
		session_start();		//Inicia seção
		//Abrindo seções
		$_SESSION['usuarioID']=$res['id'];
		$_SESSION['nomeUsuario']=$res['nome'];
		$_SESSION['email']=$res['email'];
		exit;
	}
  }
?>

Logged-in page:

<?php
session_start(); 	//A seção deve ser iniciada em todas as páginas
if (!isset($_SESSION['usuarioID'])) {		//Verifica se há seções
         	header("Location: login.html");	//Redireciona o visitante para login
}

?>

1 answer

0


You didn’t use session_start() at the top of your connection code, so it doesn’t identify anything. session_start() needs to be at the top of php code, it’s standard documentation and rule.

  • Hello! I put it on top of the PHP Connection and it’s still the same.

  • Try to put your ajax date like - date: login:login, password:password. The way you are using it is for the GET method, in this case it is POST. If it doesn’t work, maybe it’s the mysqli connection in the form of data delivery and possibly the library that is not installed from mysqli.

Browser other questions tagged

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