Search code examples
phpsessioncookiesdatatablesession-cookies

Datatable - Requested Unknown Parameter after User Inactivity


I am developing a simple role based PHP application in which Data Table is implemented. The scenario goes as The user will be signing in to the system using session and cookies. his role would be identified with the same session and cookies which matches user credentials and fetch table records as per the role. The problem I am facing is after sometime of inactivity whenever a POST request is made, I am having an error - datatables warning table id= - requested unknown parameter '0' for row 0 column 0 tn/4

Its working fine in local. I thought increasing sessions timeout might help, but unfortunately NO. its getting logged out after 30 mins although every session is set manually in codes, php.ini and CPanel's PHP Selector. I will share the code block below.

logging in

if(mysqli_num_rows($Query)==1)
{
    if(!empty($_POST["rememberme"]))
    {
        setcookie ("username", $_POST["username"], (time() + 31536000));
        setcookie ("password", $_POST["password"], (time() + 31536000));
    }
    else
    {
        if(isset($_COOKIE["username"]))
        {
            setcookie ("username", "");
        }
        if(isset($_COOKIE["password"]))
        {
            setcookie ("password", "");
        }
    }
    if (!session_id()) session_start();
    $_SESSION['adminsuccess'] = $username;
    $_SESSION['user'] = $username;
    
    //Session Management
    $_SESSION['expire'] = time();

    header('Location: admin_interface.php');
    die();
}

Then as soon as he logs in he will be redirected to admin_interface.php. the session management is handled as this there.

<?php
ini_set('session.gc_maxlifetime', 31536000);
//Session Management
if (!isset($_SESSION['adminsuccess']))
{
    header("Location:login.php");
}
else
{
    if((time()-$_SESSION['expire']) > 31536000)
    {
        session_destroy();
    }
    else{
        echo "admin user logged in";
    } 
}
?>

Then in the same class I am implementing Server Side DataTable as well which fetches the data from JSON through another class as below.

var table = $('#example').DataTable( {
    "processing":   true,
    "serverSide":   true,
    "order": [[ 1, "asc" ]],
    "paging"    :   true,
    "searching" :   true,
    "sDom": 'Brtip',
    "defaultContent": "-",
    "targets": "_all",
    "iDisplayLength"    :   100,
    "ajax": {
        url  :"fetchdatarecord.php",
        type : "POST",
    },
    "autoWidth": false,
    "aoColumnDefs": [{ "bSortable": false, "bSearchable": false, "aTargets": [1,2,3,4 ]},],
    "aoColumns": [{ "sWidth": "5%" }, { "sWidth": "5%" },{ "sWidth": "2%" }, { "sWidth": "3%" },{ "sWidth": "2%" }]
} );

Finally on fetchdatarecord I am getting the data as this with columns and set with JSON

if($role == "admin"){
    $subdata[]=$row[0];
    $subdata[]=$row[1];
    $subdata[]=$row[2];
    $subdata[]=$row[3];
    $subdata[]=$row[4];
}

Then Finally -

$json_data=array(
    "draw"              =>  intval($request['draw']),
    "recordsTotal"      =>  intval($totalData),
    "recordsFiltered"   =>  intval($totalFilter),
    "data"              =>  $data
);
echo json_encode($json_data);

All fetching records are working fine. except, as mention due to the user inactivity, the error throws up and the tables are going empty. if I reload the page it takes me to login.php.


Solution

  • The answer seems very simple as to changing the session save path on .htaccess as "/tmp". This is what helped me out. This has nothing to do with the DataTable plugin as it stops the connection when the session is timed out.