In my application i want a div popup after user login and that popup will shown every hour on time of login. div popup will shown with form of one textbox and submit and if the user do not submit any data before second div popup shown that form should be submited automaticaly with empty data and data should be stored into the data base. now the issue is how to show div popup every hour if i use inverval in javascript its only client side and on refresh the value of interval will be changed so javascript will not used.second option is to use session but when user clear histry it will loss the data .i m confused how to do this think .how i can show popup every hour after the login entry. popup showing will be continued every hour untill user do not logged out.Is it possible ?
maybe try using a session variable that will reset after a set amount.
<?php
session_start();
$inactive = 3600;
$_SESSION["timeout"] = time();
if (isset($_SESSION["timeout"])) {
$sessionTTL = time() - $_SESSION["timeout"];
if ($sessionTTL > $inactive) {
// reset the timer (*not tested)
$_SESSION["timeout"] = time() -3600;
// add popup code
}
}
?>
Related
I made a site that is user-based and so far I've been able to easily handle the sessions up to this point. I'm not a noob when it comes to php, but I don't have a good working knowledge of javascript. I have set a timeout for my users so that they are automatically timed out after 20 minutes of inactivity. It works great, other than they need to click a link or refresh in order for the session to actually be timed out. This is my current php timeout.
<?php
//TIMEOUT AFTER 20 MINUTES
if(isset($_SESSION['signed_in'])) {
$inactive = 1200;
if(isset($_SESSION['timeout']) ) {
$session_life = time() - $_SESSION['timeout'];
if($session_life > $inactive) {
$id = $_SESSION['user_id'];
//this db row is solely to display # of users logged in
mysqli_query($connect, "UPDATE users SET user_signedin='0' WHERE user_id = '$id'");
session_destroy();
$_SESSION = array();
header("Location: timedout.php");
}
}
$_SESSION['timeout'] = time();
}
?>
But I've also tried javascript (setting the timer to 5 minutes) like so:
<script>
var wintimeout;
function SetWinTimeout() {
wintimeout = window.setTimeout("window.location.href='timedout.php';",300000);
}
$('body').click(function() {
window.clearTimeout(wintimeout); //when user clicks remove timeout and reset it
SetWinTimeout();
});
SetWinTimeout();
</script>
I've searched a number of different places to find out how to implement an automatic redirect without having to click or refresh, but so far no luck. How do people redirect automatically (without clicking or refreshing) upon timeout?
EDIT: For people who are looking for the same answer: Ok, I was able to get the desired effect by adding this to my page header
<?php if(isset($_SESSION['signed_in'])) { ?>
<meta http-equiv="refresh" content="1800;url=internal.php" />
?>
"internal.php" is very similar to the first piece of code I pasted above, though I've taken out the piece where it checks for timeout. However, now I'm having problems with people exiting the tab and not getting signed out, but that's a whole other issue.
I just created a Pup-up using Magnific Pop-up. I now have to set the session rules:
The popup has to appear 5 seconds after the user lands in the website and has not to be shown for the rest of session
The popup has to appear 5 times for each user's session: as soon as the user does not perform any action on the popup or the user closes the popup 5 times, the popup doesn't have to appear anymore.
Can you help me please? Thanks in advance!
Below is a simple solution, however depending on how your site is set up other page loads and/or ajax calls could increment the counter, so be conscious of when/where you increment the session variable $_SESSION['show_popup_count']
<?php
session_start();
if(isset($_SESSION['show_popup_count'])){
//handle completely new session here
$_SESSION['show_popup_count']=0;
}
$_SESSION['show_popup_count'] += 1;
//expose value to javascript
?><script type="text/javascript">
var popupCounts = <?php echo $_SESSION['show_popup_count']; ?>;
</script><?php
.... //continue on with rest of code
then here is your additional javascript
if(popupCounts<6){
//code to show popup here
}
I'm trying to create a very simple chat system with auto refresh and refresh on submission of new chat message from the user.
I currently have two functions - one for submitting the new message, clearing the form and refreshing the chat (using the next function) called clearchat():
function clearchat() {
var frm = document.getElementById('chatform');
document.getElementById('fullmessage').value = document.getElementById('chatmsg').value;
frm.submit(); // form is being submitted to a hidden iframe.
frm.reset();
refreshchat();
}
And then the other that refreshes the chat that should be called when clearchat() runs and is also called every 3 seconds using an interval:
function refreshchat() {
$('#qcwindow').load(document.URL + ' #qctext');
$('#chatwindow').load(document.URL + ' #chattext');
var chatwindow = document.getElementById('chatwindow');
var difference = chatwindow.scrollHeight - chatwindow.scrollTop;
if(difference < 750) {
chatwindow.scrollTop = chatwindow.scrollHeight;
}
}
This function loads the new chat information into the DIV and then keeps it scrolled to the bottom of the div unless the user has manually scrolled away from the bottom of the div.
Both functions work individually. The problem I'm having is that when the user submits a new message it does submit the form and clear the form but it does not refresh the chat. The chat still automatically refreshes every 3 seconds, though. But I'd like the new message from the user to show up instantly.
I cannot figure out for the life of me why the refreshchat() function inside the clearchat() function isn't being called.
Any help would be appreciated. Thanks.
UPDATE:
I've added console_log("refrehsed") to the refreshchat() function and it gets added to the log every time both through hitting enter manually and also the auto refresh but the div only actually updates on the auto refresh.
When you submit the form to an iframe, you should wait for the post to finish before updating the UI. You can know that the form finished submitting by listening to the load event of the iframe the form is targeting.
I have 3 php files: view.php, edit.php and edit2.php.
I use view.php to display content of my DB tables, edit.php to edit a specific row (I use textboxes etc.) and edit2.php to write any changes to my database. After the query is successfully executed, edit2.php uses header("Location: edit.php"); to display the chosen row again (I use session variables in edit.php and unset them on view.php).
edit.php and edit2.php are opened in a small popup window. Now what I would like to have is when I close my small window, view.php should be refreshed.
I tried using onunload but what it does it is triggers itself each time a button is clicked in edit.php to send data to edit2.php, so when it returns to edit.php, it's blank because session variables are unset due to refreshing of view.php.
I have this strange feeling, that I might have explained it in a twisted way... Nevertheless, I would need some help.
What you want to do is open a new window using window.open('edit.php')
Here is the documentation for window.open()
Then watch for when the new window is closed, and refresh the page when that happens. Unfortunately there is no 'on close' event that's fired, but there is a property window.closed that you can watch to see when the window is closed.
So this is what you should do:
var win = window.open('edit.php','Edit Row','width=800,height=600,status=0,toolbar=0');
var timer = setInterval(function() {
if(win.closed) {
clearInterval(timer);
location.reload()
}
}, 1000);
I am working on online examination. In which the examination duration will fetch by mysql(in minutes). I want to show the countdown time on the top of the page.
currently I am using
http://javascript.internet.com/time-date/countdown-timer.html
but the main problem is .. when every time a new question display ..the page get refresh and ..timer reset again from start.
Please help me where is the main problem?
or any other example if you want to suggest?
Thanks
Before running the test you need to calculate exact test end time on the server side and save it into session:
<?php
if (empty($_SESSION['test_will_end_by'])) {
$_SESSION['test_will_end_by'] = time() + $test_duration_in_seconds;
}
?>
Then, if you provide it to your client-side script from HTML:
Time left:
<span class="timer" data-end="<?php
echo date(DateTime::RFC1123, $_SESSION['test_will_end_by']);
?>"></span>
Add this code into your jQuery DOM-ready handler to start all timers on a page:
$('.timer').each(function() {
var target = new Date($(this).data('end')), update, $this = $(this);
(update = function () {
var now = new Date();
$this.text((new Date(target - now)).toUTCString().split(' ')[4]);
if (Math.floor((target - now)/1000) == 0) return; // timer stops
setTimeout(update, 1000);
})();
});
Every time you refresh the page, the JavaScript code will run again, so the timer will be reset. To solve this problem, get the content without refreshing the page, that is : Ajax.
You can store the current value in a cookie using the form submit or page beforeunload events. Then when a new page loads, get the remaining time and keep counting down. This also allows for some network latency.
Note that the user may be able to access and modify the value of the cookie.
Store the question start time in session, when ever page refreshes get the session start time difference from server and display them in web page. The increment part you can do from the client side javascript code without using AJAX.