'Destroy session upon refresh
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;
}
?>
Solution 1:[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']);
Solution 2:[2]
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
Solution 3:[3]
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);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ScarecrowAU |
Solution 2 | Community |
Solution 3 | codeLover |