2

I am trying to destroy the session information upon refresh. The problem here is that everytime I refresh, the data still appears on the table. How do i set the table data to null upon refresh?

<?php
        $value = $_SESSION["data"];
        $query = "SELECT * FROM datas WHERE CONCAT(`code`,`name`, `ID`, `cost`) LIKE '%".$value."%'";
        $search = filterTable($query);
    if (isset($_SESSION["data"])) {
       if (basename($_SERVER['PHP_SELF']) != $_SESSION["data"]) {
            session_destroy();
       }
    }
    function filterTable($query)
    {
        $connect = mysqli_connect("localhost", "root", "", "databasename");
        $filter = mysqli_query($connect, $query);
        return $filter;
    }

?>
xodebox95
  • 93
  • 2
  • 4
  • 13
  • You can use Javascript here. You can clear your table in 'Document.ready' function. Thus, for each refresh the table will become blank. – Akhter Al Amin Jul 17 '16 at 06:22
  • maybe the problem is this condition: if (basename($_SERVER['PHP_SELF']) != $_SESSION["data"]). Try to double check this. – Marko Krstic Jul 17 '16 at 06:29

3 Answers3

1

If you want to clear all session variables after you have retrieved them once, you can use PHPs session_unset() function.

In your code it would be:

$value = $_SESSION["data"];
    $query = "SELECT * FROM datas WHERE CONCAT(`code`,`name`, `ID`, `cost`) LIKE '%".$value."%'";
    $search = filterTable($query);
if (isset($_SESSION["data"])) {
   if (basename($_SERVER['PHP_SELF']) != $_SESSION["data"]) {
        session_unset();
   }
}

This should clear all session data so when the page is refreshed, $value should be null.

You can also try to use unset() to remove the values. E.g.

unset($_SESSION['data']);
ScarecrowAU
  • 194
  • 2
  • 9
0

one alternative way is to set session as empty array, and that would help you:

$_SESSION = array();

EDIT

did you try first to destroy and then to set empty array like:

session_destroy();
$_SESSION = array();

maybe this post might help you: why session_destroy() not working

Community
  • 1
  • 1
Marko Krstic
  • 1,417
  • 1
  • 11
  • 13
-1

We cannot unset session variable through javascript or ajax. So we have to pass the control first to php using javascript and ajax. From there you can unset session data :

//put this line of code in that page you are planning to refresh.
window.onbeforeunload = function() {
      if (typeof XMLHttpRequest != "undefined"){
                xmlHttp= new XMLHttpRequest();
                }
                else if (window.ActiveXObject){
                    xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
                }
                if (xmlHttp==null){
                    alert("Browser does not support XMLHTTP Request")
                    return;
                }
                var base_url="<?php echo base_url(); ?>"
                var url=base_url + 'controller/function_name';
               // url +=id;
                xmlHttp.onreadystatechange = stateChange;
                xmlHttp.open("GET", url, true);
                xmlHttp.send(null);

  };

//your function in php will be 

public function function_name(){

        $newdata =  array(

                            'session_variable' =>0       
                    );

        $this->session->set_userdata($newdata);
    }
codeLover
  • 2,571
  • 1
  • 11
  • 27