php or javascript session rules popup - javascript

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
}

Related

How can I automatically log users session out without clicking first

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.

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.

showing popup form every hour after login in php

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
}
}
?>

Javascript refresh main page after closing child window (popup)

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

Javascript: Terminate the program when CANCEL clicked

Scenario:
I have a confirmation pop up message where when I click the OK button it will just proceed or will save the entry that I created - this is okay.. BUT when I click the CANCEL button it will go back to the page of booking_content.php - this one is okay...
PROBLEM:
the problem now is with the CANCEL, returning back to the page is good but its still saving the entry which is when I pressed theCANCELit will not save and will return back to the page ofbooking_content.PHP`.
QUESTION:
Is there a way to *terminate the program* when I click the CANCEL? I also tried the EXIT; but its not working....
Here's my code of Javascript along with PHP:
// other if else..
else if($network == "Mass and Mobile" && $row[fldTotalDuration] == "08:00:00" && $duration >= "0 s")
{?><?php
if(sizeof($bldg) == 1)
{
echo "<script type='text/javascript'>\n";
echo "alert('$bldg[$i] station is already full.');\n";
echo "window.location='booking_content.php'";
echo "</script>";
}
else
{ ?>
<script>
var r=confirm('$bldg[$i] station is already full. Do you want to save the other networks?')
if (r==true) {
alert("OKAY BUTTON");
} else {
window.location='booking_content.php';
}
</script>
<?php
}
}
//after all the if else...
else
{
// heres my code for saving the data....
}
THANK YOU IN ADVANCE...
You do realize that PHP executes on the server to generate a large string which is then sent to the browser as an HTML file that contains the javascript don't you?
So, the code executes as follows:
The user makes request to 'booking_content.php'.
The web server executes PHP to process the 'booking_content.php' script.
The 'booking_content.php' script is executed including the else section that contains the code for saving the data.
Data is saved and HTML string is generated and sent to the user.
Browser receives the HTML and executes the javascript which prompts the user with the confirmation dialog.
Do you see the problem here? The data is saved long before the confirmation dialog is shown to the user.
The way around this is to pop the confirmation dialog before the user makes a request to 'booking_content.php'. It's usually done as an onsubmit event on the form that submits the data or manually calling the dialog function if the data is submitted via ajax.
Alternatively in cases where you need to check some data or status in the server before popping the dialog you can have a separate intermediate page to ask the confirmation. For example, create a 'booking_confirmation.php' that gets the data from the form which in turn asks the confirmation and then redirects to 'booking_content.php' when the user clicks OK (remembering to include the post data or query param).

Categories

Resources