Problem with $_SESSION on login system, $_SESSION closes alone

Asked

Viewed 1,733 times

0

My login system is not working...

In my project I have a file called header.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 this link by include on all pages...

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

inserir a descrição da imagem aqui

And the menuprincipal.php has the following code:

<?php

            if(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 there is open user session, the system gives a include in the file that contains user profile. If there is no session it gives a include in the file that has login form.

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

But being that mine logout.php is under 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 friends, I am a beginner and I confess that I read it long before asking, but I find it difficult to understand this process. I count on your cooperation.

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

2 answers

0

If I’m not mistaken, on every page that checks the session, it has to be used one session_start() again, otherwise it’s like it’s closed.

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

  • I use session_start() in the.php header file so every page has this command, I used var_dump($_SESSION), and it’s OK. It shows login data, but Session gets null when the page updates.

  • Are you working locally with shaman for example? recommend that you scan your php.ini file for everything related to sesion like Session.cookie_lifetime=0, or before that, debug your php from the first line of your code, or your session is set to last very short, or you forgot somewhere a session_destroy()

  • I use Wamp Server, and on Localhost I don’t have these problems. The error happens on the online site, would it be some problem of the hosting provider ? I don’t think... Here in this forum can be available contacts? I would like to leave my Whatsapp here.

0

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

The problem is in the file configuration PHP.ini which is at the server root. This file has to be configured with the hosting standards. An example of Linux environment Locaweb:

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

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

see more here

Browser other questions tagged

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