Session values do not appear

1

Each user has a login with email and senha .

I can enter and receive user session values such as username and idade .

In other user accounts, after login success , I do not get any value from the session variables, but if I make a Print_r ($_SESSION); I can see that there is data in $_SESSION . Any tips?

In the database the users are correct.

login.php

<?php 
include('init.php');
$em = $_POST['txtemail'];
$pw = $_POST['txtpassword'];


$pdo = new PDO('mysql:host=localhost;dbname=teste', 'teste', 'teste'); 
$stmt = $pdo->prepare('select * from user where email = :email and senha =    :senha'); 

$stmt->execute(array(
   ":email" => $em,
   ":senha" => $pw,
));
if ($stmt->rowCount() > 0){
$linha = $stmt->fetch(PDO::FETCH_ASSOC);

$_SESSION['email']=$linha['email'];
$_SESSION['username']=$linha['username'];
$_SESSION['id']=$linha['id'];
$_SESSION['last_login']=$linha['last_login'];
$_SESSION['nlog']=$linha['nlog'];

header("location: ../portal/index.php");
}
else //CASO NÃO COINCIDAM
{

header("location: ../index.php?erro=1");
}



?>

init.php

<?php 
session_start();

//CONN DB
include('conn.php');
if(!isset($_SESSION['start_login'])) { 
 $_SESSION['start_login'] = time(); 
 // adiciona 30 segundos ao tempo e grava em outra variável de sessão
 $_SESSION['logout_time'] = $_SESSION['start_login'] + 30*60; 

}
// se o tempo atual for maior que o tempo de logout
if(time() >= $_SESSION['logout_time']) { 
header("location:php/logout.php"); //vai para logout

} else {
 $red = $_SESSION['logout_time'] - time(); // tempo que falta
 //echo "Início de sessão: ".$_SESSION['start_login']."<br>";
 //echo "Redirecionando em ".$red." segundos.<br>";
}
?>

At the top of the reserved pages I have:

<?php 
include('php/init.php'); 

if (!isset($_SESSION['id'])) //SE n EXISTIR AUTENTICAÇÃO
{
header("location: ../index.php?erro=1");
}
//Print_r ($_SESSION);
?>
    
asked by anonymous 08.04.2016 / 00:39

1 answer

2

Whenever you request a session data you should give session_start() before. Ex:

session_start();
if(!isset($_SESSION['user'])){
   header("Location:index.php?error=denied");
} 

Whether you are going to authenticate, retrieve a session data, change it or even destroy the session, you must first use the session_start() method on all the pages you will use session data

    
08.04.2016 / 14:34