Call in a php function when Jquery timer reach to 0 - php

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');
}

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');
}
});

Related

Automatically log out user from WordPress dashboard after 15 minutes of inactivity

I wanted to log users out of the back office after 15 minutes of inactivity.
With my code, the disconnection works but also if I am active.
function myplugin_cookie_expiration( $expiration, $user_id, $remember ) {
return $remember ? $expiration : 900;
}
add_filter( 'auth_cookie_expiration', 'myplugin_cookie_expiration', 99, 3 );
Any tips?
Should should use jQuery to detect idl time.
Check this: How to detect idle time in JavaScript elegantly?
Then you use Ajax to load you PHP script.
So you will have something like that:
var idleTime = 0;
$(document).ready(function () {
//Increment the idle time counter every minute.
var idleInterval = setInterval(timerIncrement, 60000); // 1 minute
//Zero the idle timer on mouse movement.
$(this).mousemove(function (e) {
idleTime = 0;
});
$(this).keypress(function (e) {
idleTime = 0;
});
});
function timerIncrement() {
idleTime = idleTime + 1;
if (idleTime > 14) { // 15 minutes
$.ajax({
url: 'logout.php',
success: function(data) {
window.location.reload();
}
});
}
}
And in your logout.php just call the function to logout actuel user

How do I display the amount without refresh?

I am using CodeIgniter.
I have a function called as reloadCart(). I have to reload this function every time because this function will reload the amount anything changes happened. This is working perfect but the issue is when I am refreshing the page then it displays the new amount.
$(document).ready(function(){
reloadCart();// function reload on page refresh
function reloadCart(){
$.ajax({
url: "<?php echo base_url(); ?>Member_controller/primaryCartload",
context: document.body,
success: function(data){
if (data !=0) {
var obj = JSON.parse(data);
if (obj.qty != 0) {
$('#totalDetails').html(obj.cart_total);
$('#totalQty').html(obj.totalQty);
}
}
else{
//alert('empty')
$('#totalDetails').html('0');
$('#totalQty').html('0');
}
}
});
}
});
Can we use something like?
load({
reloadCart();
});
You can just update your price by running that AJAX call time to time, for that you'll need to set a time after that specific time your call will be run again.
var count = 20;
setInterval(function () {
count--;
if (count === 0) {
count = 20;
reloadCart();
}
}, 1000);
So after every 20 sec this function will be called, you can amend it as required.

Implementing Timer in PHP and Sleep function

I am creating an PHP application in which i want to set a timer of 15 minutes for a particular section of test. If the student doesn't complete the section in 15 minutes the scores are saved and the student is navigated to the next section.
So i want to create a timer and want to display the current time left. But i don't know how to implement it and also if we use sleep() function that will it interrupt the working of other functions as well.
You can't use PHP for this. If you do, then it will simply freeze the page on page load for 15 seconds and then tell the student time has expired.
So, you should write it in JavaScipt like this:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
currentMinute = 0;
myTimer = setInterval(function () {
if (currentMinute > 15) {
clearInterval(myTimer);
//send post data that the user's time is up
return;
}
$.post("sendData.php", {
"timeIsUp" : true
}, function (data) {
console.log("done sending time has expired to server");
});
currentMinute++;
}, 60 * 1000);
</script>
jQuery will help you on this...
Use below function for timer
var hours1=0, minutes1=0, seconds1=0;
function calculate() {
setTimeout(calculate, 1000);
if((hours==0)&&(minutes==0)&&(seconds==0)){$('#submit-btn').trigger('click');} // HERE YOU CAN SWITCH TO NEXT QUESTION
h1 = makeTwoDigit(hours); m1 = makeTwoDigit(minutes); s1 = makeTwoDigit(seconds);
h2 = makeTwoDigit(hours1); m2 = makeTwoDigit(minutes1); s2 = makeTwoDigit(seconds1);
$('title').html(h1+':'+m1+':'+s1);
$('#time-taken').val(h2+':'+m2+':'+s2);
$('#minutes').html(m1);
$('#seconds').html(s1);
seconds--;
if (seconds < 0) {
seconds = 59; minutes--;
if (minutes < 0) {
hours--;
minutes = 59;
}
}
seconds1++;
if (seconds1 > 59) {
seconds1 = 00; minutes1++;
if (minutes1 >59) {
hours1++;
minutes1 = 00;
}
}
}
function makeTwoDigit(num) {
if (num < 10) { return "0" + num;} return num;
}
Thanks

Refresh Div at a certain time

Ok so I am creating a radio player and basically I need the Title, Content div, Next show Div to refresh at certain times for example 9am then 12pm. I have the JQuery code to refresh the page at a certain time but that isn't quite what I'm after. Any ideas?
Code:
function refreshAt(hours, minutes, seconds) {
var now = new Date();
var then = new Date();
if(now.getHours() > hours ||
(now.getHours() == hours && now.getMinutes() > minutes) ||
now.getHours() == hours && now.getMinutes() == minutes && now.getSeconds() >= seconds) {
then.setDate(now.getDate() + 1);
}
then.setHours(hours);
then.setMinutes(minutes);
then.setSeconds(seconds);
var timeout = (then.getTime() - now.getTime());
setTimeout(function() { window.location.reload(true); }, timeout);
}
Then I just call the refreshAt function by inserting the following on my page
<script type="text/javascript">refreshAt(04,30,0);</script> //page refreshes at 4:30am.
So this refreshes the Whole page. I just need to refresh the Title, and 2 divs.
What do I need to add/change in the code.
This will call your current URL after interval elapse and reset your title and your div content.
You have to write something like this:
function refreshAt(hours, minutes, seconds) {
var now = new Date();
var then = new Date();
if(now.getHours() > hours || (now.getHours() == hours && now.getMinutes() > minutes) || now.getHours() == hours && now.getMinutes() == minutes && now.getSeconds() >= seconds) {
then.setDate(now.getDate() + 1);
}
then.setHours(hours);
then.setMinutes(minutes);
then.setSeconds(seconds);
var timeout = (then.getTime() - now.getTime());
setTimeout(function() {
$.get( window.location.pathname, function( data ) {
$('title') = $(data).filter('title').text();
$('.DIV_CLASS') = $(data).filter('.DIV_CLASS').html();
alert( "Load was performed." );
});
}, timeout);
}
NOTE: This script use jQuery, so please include latest jQuery library.
You can try this:
setTimeout(function() {
document.title = "new title";
$(".divname").text("new div text");
}, timeout);
Please try below code :
setTimeout(function(){
$('#divid').load("pageurl #divid" >*",function(){
});
}, 6000);
Your div reload after 6 second.
You can take a look at jQuery's load method.
By using is you can load the content of you div.
setTimeout(function() {
$("#youfirstdivid").load("url1");
$("#youseconddivid").load("url2");
$('title').text("new title");
}, timeout);
Don't forgot to wrap your code inside
$(function(){
//you code goes here
});
and if you want to refresh it at some fixed interval then you can use setInterval rather than setTimeout

Can this javascript function be put into ajax? and how?

I am trying to put this into ajax so everytime a person clicks a button in jquery, it creates a php session. Is it possible just using this?
var milisec=00;
var seconds=60;
function display(){
if (milisec<=0){
milisec=9
seconds-=1
}
if (seconds<=-1){
milisec=0
seconds+=1
}
else {
milisec-=1
document.getElementById('counter').innerHTML= seconds+":"+"0"+milisec+"s";
setTimeout(display,100)
}
if (seconds < 10) {
document.getElementById('counter').innerHTML= "0"+seconds+":"+"0"+milisec+"s";
}
}
display()
I need to post the time it took for them to get to the next page using Php sessions, using ajax.
var seconds = 60;
var milisec = 0;
var stop_counter = false;
function display(){
if(milisec == 0)
seconds = seconds - 1;
milisec = milisec - 1;
if(milisec == -1)
milisec = 10;
$('#counter').html((seconds < 10 ? "0" : "") + seconds + ":0" +milisec+ "s");
if( (seconds == 0 && milisec == 0 && stop_counter == true) == false )
setTimeout(function(){ display(); }, 100);
}
$(document).ready(function(){
display();
$('#button').click(function(){
$.post('myphpfile.php', {time : $('#counter').text()}, function(data){
alert('Data from myphpfile.php: ' + data);
stop_counter = true; //stop counter now
});
});
});
myphpfile.php
echo 'you clicked button when counter was ' . $_POST['time'];
//you can do any php stuff here
this code should do what you want but I did not test it.
function $.post() from jQuery library will pass your data to php script and also can retrieve some data which you can display on your site or something.
btw, one second has 1000 miliseconds, not 100
sorry for my english
Yes you can do it by passing the value of "seconds" and "milisec" in the ajax call by
$(document).ready(function(){
$('#button').click(function(){
$.post('session_file.php', {secs : seconds,millisecs:milisec}, function(data){
});
});
});
So in the session_file.php you can write the values of the variables of secs and millisecs in a session

Categories