0

Consider it as the user will login and only giving 45 minutes. How to call a PHP function which is log-out when the Jquery timer runs to 0.

Here's my Code

<script type="text/javascript">

function startTimer(duration, display) {

var timer = duration, minutes, seconds;

setInterval(function () {

    minutes = parseInt(timer / 60, 10)

    seconds = parseInt(timer % 60, 10);

    minutes = minutes < 10 ? "0" + minutes : minutes;

    seconds = seconds < 10 ? "0" + seconds : seconds;

    display.text(minutes + ":" + seconds);

    if (--timer < 0) {
        timer = duration;
    }


}, 1000);
}

 jQuery(function ($) {
var FortyMinutes = 60 * 45,
    display = $('#time');
startTimer(FortyMinutes, display);
});

</script>

And here's my php function

        public function logout() {


        session_destroy();
        redirect('Authentication');
        }
Dave
  • 172
  • 1
  • 3
  • 11
  • 1
    And where's your question? – Patrick Q Mar 20 '19 at 21:14
  • 3
    since i can edit your js at any time in my browser, dont rely on this for anything important –  Mar 20 '19 at 21:15
  • 2
    I just can't move past `var FortyMinutes = 60 * 45` – ficuscr Mar 20 '19 at 21:16
  • @PatrickQ my question sir is, How to call the log-out function when the jquery timer runs 0 – Dave Mar 20 '19 at 21:17
  • i edited my comment now sir @PatrickQ – Dave Mar 20 '19 at 21:18
  • 2
    why not set the session time-out to 45 minutes, that's its job. https://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes –  Mar 20 '19 at 21:19
  • If you must call the function from jQuery/javascript take a look at this question: https://stackoverflow.com/questions/2269307/using-jquery-ajax-to-call-a-php-function – tshimkus Mar 21 '19 at 00:37

1 Answers1

1

You will have to make a request to a new page.

Since PHP is a server side language you will have to go to another page to run any PHP code. This can be done by having a logout.php file:

<?php
    logout();

    public function logout() {
      session_destroy();
      redirect('Authentication');
    }
?>

On the client side you can make an ajax request to that page. Run the following code after the timer is up.

$.ajax({
    type: "GET",
    url: 'logout.php',
    success: function(data){
        alert('Logging you out');
    }
});
Haley Mueller
  • 487
  • 4
  • 16