简体   繁体   中英

How to implement auto session timeout using Angular.js,PHP and Mysql

I need one help.I have a login module using Angular.js , PHP and MySQL. Here I need when user is not doing anything inside the site after 15mins it will auto logout and session will destroy.I am explaining my code below.

$http({
         method: 'GET',
         url: 'php/Login/session.php',
         headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
     }).then(function successCallback(response){
         //console.log('session',response);
         $scope.userType=response.data[0].first_name+" "+response.data[0].last_name;
     },function errorCallback(response) {
        $state.go('/',{}, { reload: true }); 
     });
     $scope.logout=function(){
         $http({
         method: 'POST',
         url: 'php/Login/logout.php',
         headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
     }).then(function successCallback(response){
         //console.log('session',response);
         //alert(response);
     },function errorCallback(response) {
         //console.log('session',response);
         //alert(response);
     });
     }

logout.php:

require_once '../../include/dbconfig.php'; 
$postdata = file_get_contents("php://input");
session_unset();
session_destroy();
if(session_destroy()){
    echo "User logged out successfully";
}else{
    echo "User could not log out successfully";
}

session.php:

require_once '../../include/dbconfig.php'; 
$result = mysqli_query($connect, "SELECT * FROM db_user WHERE user_id=". $_SESSION["admin_id"]);
$data = array();
while ($row =mysqli_fetch_assoc($result)) {
  $data[] = $row;
}
    if($data){
    print json_encode($data);
    }else{
        header("HTTP/1.0 401 Unauthorized");
        print "session has destroyed";
    }

dbconfig.php:

<?php
$lifetime=900;
session_set_cookie_params($lifetime);
session_start();
$con = mysql_connect("localhost", "root", "********");
mysql_select_db('go_fasto', $con);

$connect = mysqli_connect("localhost", "root", "*******", "go_fasto");
?>

Please help me to resolve this problem.

 $lifetime=900; session_set_cookie_params($lifetime); session_start(); 

here 15 min is eual to 900 sec...so mention the maxlife time to the session before starting...so after 15 min if no action performed then session will expire

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM