1

I have set session_start() on every page. Now, I want to unset session

 <div style="text-align: center;">
     <a href="clearorder.php">Clear order</a>
      </div>

Here is my clearorder.php

<?php
session_start(); 
$_SESSION["cart"] = array();
session_destroy(); 
header('location:'.$_SERVER['HTTP_REFERER']);
?>

But the session is not clearing and displaying the data of the session. What could I be missing?

Nah
  • 1,690
  • 2
  • 26
  • 46
Rahman
  • 31
  • 4
  • http://php.net/manual/en/function.session-destroy.php – u_mulder May 24 '18 at 07:17
  • First of all there might be something wrong with your logic, as you are assigning an empty `array()` to your session `cart` variable, it will always be empty. Try to assign it something, and then see if the value appears after you `session_destroy();` Also refer to this: https://stackoverflow.com/questions/6472123/why-session-destroy-not-working – Nah May 24 '18 at 07:18
  • What session data is it still displaying ? – nice_dev May 24 '18 at 07:21
  • Is that the whole code of clearorder.php? – Karlo Kokkak May 24 '18 at 08:26

4 Answers4

0

First Destroy Session and then set an empty array so it will be clear,

session_destroy();
$_SESSION = array(); // Clears the $_SESSION 
0

Just use below code after clicking the below code on that page your rdirecting after click on logout button:

Session_start(); 
$_SESSION['cart'] = array();
session_unset();
session_destroy();
Therichpost
  • 1,759
  • 2
  • 14
  • 19
0

you can try this.i hope it will help you

session_destroy();
        redirect(base_url('login'), 'refresh');
PHP Geek
  • 3,949
  • 1
  • 16
  • 32
0

The way i got my log out button to work was simply

"Button that leads to (Example: logout.php)"
<?php
Session_start();
Session_destroy();
Header("Location: "Whatever page you want" ");
?>
D3nj1
  • 95
  • 10