Create and destroy session

Asked

Viewed 246 times

1

Good morning !

I have a question about Session. For example, I am developing a system where I have 3 environments. The first is the virtual store environment and the other two environments are virtual type offices, Resellers and Retailers. These last two are restricted areas. So, do I need to create a session for each of these environments when the various users access it? That is, each user needs to generate a single session at each access, after I destroy it? For example, I am facing a problem in my shopping cart. The online store contains a shopping cart. When closing, I need to destroy the session, but when I finish my shopping cart and generate the.pdf invoice, then I destroy the session, but it doesn’t destroy it.

With respect to the shopping cart I’m doing so:

cart.php - where create the session

Destroying the cart session after generating invoice.pdf: Still inside the cart.php file //destroying the Session cart... include ("destroi_sessao_carrinho.php");

file: destroi_sessao_carrinho.php

I need a light. where I’m going wrong. Thanks in advance.

  • this file destroi_sessao_carrinho has what inside?

1 answer

2


Creating a restriction level in the users table, for example, you get the result. When the user logs in, you search for this level of access and create the session with it:

$_SESSION['nivel_acesso']=$nivel_acesso;

On the pages where it cannot enter you do a check:

if((!isset($_SESSION['nivel_acesso']))||($_SESSION['nivel_acesso']!=1)):// 1 seria o nivel necessario
    header('location: redirecionamento');//aqui você redirecionada para onde ele vai caso não tenha nivel de acesso
endif;

In the archive destroi_sessao_carrinho.php:

<?php
session_start();//session_start() deve ser chamado antes de tudo
if(isset($_SESSION['carrinho'])):
    unset($_SESSION['carrinho']); 
    session_destroy(); 
    header("Location: produtos.php"); 
    exit();
endif;
?>
  • 1

    Destroying the cart session after generating the invoice.pdf: Still inside the cart file.php //destroying the cart... include ("destroi_sessao_carrinho.php"); file: destroi_sessao_carrinho.php <? php //destroying the Session... if(!isset($_SESSION['cart']))session_start() unset($_SESSION[['cart']); session_destroy(); header("Location: products.php"); Exit; ?>

  • edited the answer

  • 1

    Thank you for learning!

  • I who thank you for being able to help, signals the answer please. Gratitude.

  • What should I flag? Close this topic?

  • No, mark the answer as correct only

  • In flag, there are only 3 options...

  • Signal no, I said wrong, mark her, as correct answer

Show 3 more comments

Browser other questions tagged

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