-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTimer2.php
61 lines (51 loc) · 1.5 KB
/
Timer2.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
session_start();
$_SESSION['timer']=(isset($_SESSION['timer']))?$_SESSION['timer']:time();
if(time()-$_SESSION['timer']>=30){
unset($_SESSION['timer']);
//code to clear all items in the cart
echo "<script>alert('Time is out.Directing to Home Page');</scipt>";
header("Location: index.php");
exit();
}
//Each time when user enters the shopping cart, we will count the remaining time.
$remaining_time=30-(time()-$_SESSION['timer']);
?>
<html>
<!--Container of timer-->
<p style="border:thick solid red; width:500px; height:50px; font-weight:bold;text-align:center;color:red;font-size:1.2em;padding-top:10px;" id="timer">
</p>
</html>
<script>
var element = document.getElementById('timer');
var counter = <?php echo $remaining_time; ?>;
var terminate = false;
var no_extension = false;
//set the timer
var x = setInterval(function() {
if (terminate != true) {
//timer to 0
if (counter <= 0) {
terminate = true;
}
if (!no_extension && counter <= 15) {
if (confirm("Do you want to extend the session?")) {
window.location.href = 'Extend_timer.php';
no_extension = true;
} else {
no_extension = true;
}
}
var minutes = parseInt(counter / 60);
var seconds = counter - 60 * (minutes);
//timer counts down
element.innerHTML = 'Remaining time to check out ' + minutes + ' m ' + seconds + ' s ';
counter -= 1;
}
//reload to activate php code.
else {
window.location.reload();
}
},
1000);
</script>