Php does not assign value in $ _SESSION

0

This registration system, I have a button to delete the product in BD'

<form action="remove-produto.php" method="post">
    <input type="hidden" name="id" value="<?=$produto['id']?>">
    <button class="btn btn-danger">remover</button>
</form>

When it goes to remove product it does this:

 $id = $_POST['id'];
 removeProduto($conexao, $id);
 $_SESSION["success"] = "Produto removido com sucesso";
 header("location:produto-lista.php");
 die();

It normally deletes and returns the perfectly to product-list.php that there is this stretch waiting

<?php if(isset($_SESSION["success"])) { ?>
    <p class="alert-success"><?= $_SESSION["success"]?></p>
<?php  } ?>

I've already tried to add SESSION_START() , which is already on another page, the login. I saw with var_dump and it says $_SESSION["success"] is null.

  

PHP Version 5.6.15

     

Xampp 3.2.2

The session is enabled and global variables too.

    
asked by anonymous 28.12.2015 / 14:21

2 answers

3

Before you assign this:

$_SESSION["success"] = "Produto removido com sucesso";

Put a session_start() and when you print too, you have to use session_start() on every page where you need to use session.

    
28.12.2015 / 14:33
1

The correct order to write to the session in PHP is:

session_start();
$_SESSION["success"] = "Produto removido com sucesso";
session_write_close();

The last command is to save and close the session.

If you want to read the session, consider:

session_start();
echo $_SESSION["success"];
    
28.12.2015 / 14:47