$_SESSION returning null

Asked

Viewed 294 times

0

I’m saving on some valuables using $_SESSION.

<?php
session_start();
include_once '../controller/LocacaoController.php';
if(!empty($_REQUEST['idfilme']) and !empty($_REQUEST['idfita'])){
  $idfilme = $_REQUEST['idfilme'];
  $idfita = $_REQUEST['idfita'];

  $_SESSION['codfilme'] = $idfilme;
  $_SESSION['codfita'] = $idfita;
  echo "entrou aqui";

}
else {

  if(!empty($_SESSION['codfilme']) and !empty($_SESSION['codfita'])){
      $_SESSION['codfita'] = null;
      $_SESSION['codfilme'] = null;
  }
}

?>

The code is as follows: if in my other file I pass values to $_REQUEST['idfilme'] and $_REQUEST['idfita'] then he enters the IF and keeps it in Sesssion, Now in case the $_REQUEST are empty he goes to ELSE and adds NULL as $_SESSION['codfita'] and $_SESSION['codfilme']

Ai in another file recover these values:

<?php
  session_start();
  include_once '../controller/LocacaoController.php';


  $idCliente = $_SESSION['cliente'];
  $idFita = $_SESSION['codfita'];

  $idFilme = $_SESSION['codfilme'];
  var_dump($idFita);
  var_dump($idFilme);

  if(!empty($_SESSION['codfita'])){

    $idFita = $_SESSION['codfita'];
    $idFilme = $_SESSION['codfilme'];
  }

  ?>

Only the problem is that even though I pass values to $_REQUEST['idfilme'] and $_REQUEST['idfita'] and entering the IF from Code 1, on the other screen it is returning null.

NOTE: Do not worry about $_REQUEST, and linking the files because it is working properly, the problem is the $_SESSION.

  • Try to remove your include at first just to test. Maybe you are starting up again or setting something up unexpectedly.

  • session_start(); Must be on the first line before any include

1 answer

2


I think you’ll solve it very simply:

<?php session_start();

Put session_start(); as a first thing, first and only once, in both your files.

  • still returns null, type in code 1, in IF if if I put an ECHO to msotrar if it is storing inside the SESSION, it shows the values, but already in the other file, it returns in one as n had entered in the IF

  • In your updated code session_start() is not in the beginning. can you put it perfectly in the beginning? Take advantage and replace the ANDfor &&

  • I think I already found the error, see code 1 when I pass values to $_REQUEST have value, they enter the right IF ? only that in this same file of code 1, I have a button "SUBMIT" that of the screen Reload, causing it to enter in Else. and passing NULL pras sesssion

  • A tip then is to replace the echo "chegou aqui";for print_r($_REQUEST); so showing what’s coming to your $_SESSION

Browser other questions tagged

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