'Contact info requirement after 5 minutes

I have a PHP site that the client would like to require the viewer to fill out and submit a contact form after viewing the site for 5 minutes. Info does not need to be added to a database, just sent to their office email. I have created the form but am not sure of the best way to handle the pop-up and make it required before continuing to view the site.



Solution 1:[1]

You're doing your client a significant disservice if you haven't tried to talk them out of this. If they want people not to visit the site, they should just not have it developed. If they want people to visit it (or ever come back) they shouldn't even consider this.

That being said, you could set up some sort of timer to "force" a popup to appear, but many popup blockers would handle that. There would be ways around any scheme like this, regardless of how you do it (cookies, serving a different page, etc.), but unless you've got something incredibly unique on that site, your client's readers probably won't even bother with them-they'll go to a site that isn't deliberately broken.

I would suggest to your client that they instead put in a box where users who WANT to be contacted can enter an email address. That won't annoy your users, but will still let you take in leads from those who are interested.

Solution 2:[2]

This may help you;

PHP for the website page

if (false === isset($_SESSION['startTime'])) {
     $_SESSION['formSubmitted'] = false;
     $_SESSION['startTime'] = time();
}

PHP for the jqueryCheck.php;

$return = 0;

if (false == $_SESSION['formSubmitted']) {
    if ((time() - $_SESSION['startTime']) > 300) {
        $showForm = 1;
    }
} else {
    $return = 2;
}

echo $return;

PHP after form submitted and validated

$_SESSION['formSubmitted'] = true;

JQUERY

var intervalId = setInterval(function(){
    $.ajax({ url: "jqueryCheck.php", success: function(showForm){
        switch (showForm) {
             case 0: //not yet at 5 mins and not submitted
             break;
             case 1:  //at 5 minutes no form submitted
                //code to show form
             break;
             case 2:  //form submitted
                 clearInterval ( intervalId );
             break; 
        }
    }});
}, 60000);

Solution 3:[3]

This is a simply solution without Javascript.

if (!isset($_SESSION['trial_end'])) {
    $_SESSION['trial_end'] = time() + 5 * 60;
}

// Check if trial has expired and we are not already on the form
if (time() > $_SESSION['trial_end'] && $_SERVER['REQUEST_URI'] !== '/form') {
  header('Location: /form');
  exit;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Todd Allen
Solution 2 Jason Brumwell
Solution 3 Znarkus