How can I automatically log users session out without clicking first - javascript

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.

Related

Redirect to another page on page load but only after a check is made

I am building a feature whereby a user can only view a job that they're given if they've agreed to the HIPAA guidelines. If they have already agreed, the job page will load as normal onclick, but if not, they should be redirected to the guidelines page.
I have tried a page redirect, but that gives me a 404 error. I have my program (PHP) checking a field in the database; if it's 1 then they have agreed and the job page should load as normal. If it is 0 then they should be redirected to the HIPAA page. I am calling a function on checking the database field:
<script type="text/javascript">
function hipaa() {
var referringUrl = window.location.href;
window.location.href = 'user/hipaa';
}
</script>
And
if ($result[0]->hipaa == 0) {
$url = $_SERVER['PHP_SELF']; // current page so user can return
hipaa(); // EDIT: removed this line
//Added these lines:
$urlNew = '/user/guidelines';
echo '<META HTTP-EQUIV-REFRESH CONTENT = "1; ' . $urlNew . '">';
}
The function gets called but the window.location.href gives me a 404 error. Any help would be much appreciated. I am a relative beginner in JavaScript.
EDIT: I updated my code to try and do it through PHP and I don't get an error now, but it doesn't redirect, it just stays on the same page.
Your META tag should look like this....
<?php
$urlNew = 'https://stackoverflow.com/questions/53988586/redirect-to-another-page-on-page-load-but-only-after-a-check-is-made';
echo '<meta http-equiv="refresh" content="1;URL=\'' . $urlNew . '\'" />';
?>
But if you look at the W3C standard, even way back to (3.2) pre-97, it states that redirects should always "when possible" be issued server-side or by giving the visitor a link that is directly clickable.
I ended up using JavaScript and after playing around for a bit, got it to work. My code is:
echo '<script type="text/javascript">window.location.href = "../guidelines";</script>';
Thank you to all who took the time to respond.

php or javascript session rules popup

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
}

How to refresh the WordPress home page without user noticing?

I would like to refresh the home page every X sec. I found this solution:
Install Auto Refresh Single Page plugin
Insert to header.php the lines:
if (is_front_page()) {
echo '<META HTTP-EQUIV="REFRESH" CONTENT="5">' ;
}
It refreshes the page but I notice that the page is reloaded. I would like it to happen smoothly. I don't want the refresh to make the page disappear for milliseconds. I want to refresh without the user noticing.
I have a page build with Wordpress. This page have few divs. Part of them list post type - and I need i to updated in case a new post submitted. Some divs need to change according to time of the day or the week and display different data. The page need to show all the time like a TV and information should be update automatically with no human touch. I tried to use ajax, but I don't know how to point it to reload specific div that built from Wordpress - I have no URL for specific div.
Check this out
if (is_front_page()) {
?>
<script>
setTimeout(function() { window.location = ""; }, 1000); //1 sec = 1000
</script>
<?php
}
I read your question, And as i thinking, you want update your posts on users homepage without any reloading. This can be done using "AJAX PHP". You should find plugins that uses "AJAX PHP" or "AJAX JAVASCRIPT".I hope this would be useful for you.

Can't get cookie expiration to set

Can't get cookie expiration to set
I'm using php to set a cookie on a wordpress site, but having a bit of difficulty as the content shows up every time the page is loaded instead of only showing up the first time a user visits the site (expires once a day).
Forgive me if this is obvious, I'm still learning ;)
Here's the function, I have this in my functions.php page:
function set_newuser_cookie() {
if ( !is_admin() && !isset($_COOKIE['jtc_newvisitor'])) {
setcookie('jtc_newvisitor', 1, time()+60*60*24, COOKIEPATH, COOKIE_DOMAIN, false);
}
}
add_action( 'init', 'set_newuser_cookie');
Here is how I'm calling it:
<?php if (isset($_COOKIE['jtc_newvisitor'])) { ?>
<div id="modal-notices">content</div>
<?php } else { } ?>
Here's my js, I'm using to have the content display when the page loads:
$(window).load(function(){
$('#modal-notices').modal('show');
});
I'd like to have the content load if it's the first time the user has visited the page within 24 hours, but as of now, it just loads every time.
If anyone can point me in the right direction here or explain what I'm doing wrong, it would be much appreciated.
Thanks!
It looks like you're checking if the cookie is set rather than if it's not set. Try this as your if statement:
if (!isset($_COOKIE['jtc_newvisitor']))

Countdown timer-php+mysql+js

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.

Categories

Resources