0

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!

pacc88
  • 67
  • 1
  • 10

1 Answers1

1

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
}
Jpsh
  • 1,697
  • 12
  • 17
  • Hi user3299379 thanks for your answer, I am not very practical so could you help me again? I installed the plugin magnific popup, and this is the code that I included in index.php: https://pastebin.com/GpQHZbsK Sorry if I'll put it on pastebin, but I can not format it correctly, It is divided into html and javascript. – pacc88 Apr 03 '17 at 15:09
  • perfect thank you, now how I combine my code (HTML and Js) with the code that you gave me? – pacc88 Apr 03 '17 at 16:18
  • It works thanks. the problem is that the session count is also incremented when I move among the pages, then load each time the popup. What I want is that when I enter the site I charge the popup and all the time that I stay inside must not be loaded, it increased the count, only when I leave the site and return must apply the count. The code I put in index.php. Sorry for my English – pacc88 Apr 04 '17 at 14:36
  • 1
    https://pastebin.com/2rZuYYDq if this answers your question please mark it best answer http://stackoverflow.com/help/someone-answers – Jpsh Apr 04 '17 at 15:52
  • Thanks user3299379, all your answers have been very useful. Other two questions, you can destroy the session when I close the tab for that site and not only when I close the browser? Is there a way to maintain stored the session count even when I exit the browser and destroy the count, for example, after 6 months? Thanks again. – pacc88 Apr 05 '17 at 09:16
  • 1
    php makes the session by storing it in a cookie, the browser will remember it even if the session has expired on the server, and when will assign new session. if you want to control the session time look at this answer http://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes – Jpsh Apr 05 '17 at 15:39
  • I guess the condition in `if(isset($_SESSION['show_popup_count']))` should be negated, so it should be like this `if(!isset($_SESSION['show_popup_count']))` – Risinek Mar 21 '18 at 23:27