$ _SESSION login problem, $ _SESSION ends alone [duplicate]

0

My login system is not working ...

In my project I have a file called cabecalho.php

<?php
ob_start(); //abrir sessao
session_start();//iniciar sessao
error_reporting(0); //ignorar alguns erros

    include("Pdo/conexao.php"); //conexao com banco de dados
    include("includes/logout.php"); //arquivo pra delogar usuario
?>

<!DOCTYPE html>
<html> ...

This header.php is linked by include on all pages ...

My menu contains a button in the upper right corner to open the form.

Andmenuprincipal.phphasthefollowingcode:

<?phpif(isset($_GET['acao'])){if(isset($POST['logar'])){$acao=$_GET['acao'];if($acao=='negado'){header("Location: login.php?acao=acessonegado");
                }
              }
            }
            //se EXISTIR usuario e senha logados
            if(isset($_SESSION['USUARIOCFSITE']) && (isset($_SESSION['SENHACFSITE']))){

              include 'includes/usuariologado.php';              
            }else{

              if(isset($_POST['logar'])){

                $usuario = trim(strip_tags($_POST['usuario']));
                $senha = trim(strip_tags($_POST['senha']));

                if ($usuario == "admin" and $senha == "123123") {

                  //recuperar o POST (oq usuario digitou)
                  $usuario = $_POST['usuario'];
                  $senha = $_POST['senha'];
                  $_SESSION['USUARIOCFSITE'] = $usuario;
                  $_SESSION['SENHACFSITE'] = $senha;

                  include 'includes/usuariologado.php';

                }else{
                  include 'includes/usuariodesconectado.php';
                }

              }else{
                include 'includes/usuariodesconectado.php';
              }
            }

            ?>

My logic is: if an open user session exists, the system will include an include in the file that contains the user's profile. If the session does not exist, it will include an include in the file that has a login form.

Until then login works! However, when accessing another page, the user is logged out. It's like the session ends.

But my logout.php is about the condition of a request!

<?php
    if (isset($_REQUEST['sair'])) {
        session_destroy();
        session_unset( $_SESSION['USUARIOCFSITE'] );
        session_unset( $_SESSION['SENHACFSITE'] );
        header("Location: login.php?acao=logout");
    }   
?>

My dear, I am a beginner and I confess that I read a lot before asking, but I find it difficult to understand this process. I count on your cooperation.

P.S. An important note is that ... this error happens only with the project that is hosted on the web, and in LOCALHOST works perfectly.

    
asked by anonymous 06.09.2017 / 18:27

2 answers

0

If I'm not mistaken, on every page that is to check the session, it has to be used a session_start() again, otherwise it is as if it were closed.

To confirm my tip, from a var_dump($_SESSION) before and a session_start() , you will see that the first one will null .

    
06.09.2017 / 18:36
0

"PS An important observation is that ... this error happens only with the project that is hosted on the web, and in LOCALHOST it works perfectly." p>

The problem is with the configuration of the PHP.ini file that is in the root of the server. This file has to be configured with the hosting standards. An example of locaweb for linux environment:

session.save_path = "/home/SEU_LOGIN_FTP/tmp"

In the case of locaweb the area "SEU_LOGIN_FTP" has to be changed with the FTP login to work properly. Check the configuration of your hosting server.

see more here

    
07.09.2017 / 03:38