在特定时间段内用户访问网站后创建弹出窗口

时间:2018-11-29 14:45:59

标签: php mysql datetime session

我正在我的网站上创建一个弹出窗口,尝试让用户注册我们的邮件列表。

通过在我的所有页面中都包含此php文件,当他们在网站上导航超过一分钟时,就会显示弹出窗口。

但是,如果他们停留在一页上并且不单击鼠标,我仍然希望它弹出而无需更改页面或刷新当前页面。

<?php
session_start();

if(isset($_POST['mailing_list_input']) && $_POST['mailing_list_input']!=""){
    $email = $_POST['mailing_list_input'];
    if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $params = [$email];
        $sql = "INSERT INTO mailing_list(email, date_time) VALUES (?,now())";
        $stmt = DB::run($sql,$params);

        $date = new DateTime();
        unset($_SESSION['timer']);
        $_SESSION['already_asked'] = $date;
    }else{
        $email_warning = "<div id='warning'>Email address is not valid</div>";
    }
}

if(isset($_POST['mailing_list_hide']) && $_POST['mailing_list_hide']=="hide"){
    $date = new DateTime();
    unset($_SESSION['timer']);
    $_SESSION['already_asked'] = $date;
}

if(!isset($_SESSION['timer']) && !isset($_SESSION['already_asked'])){
    $date = new DateTime();
    $date->modify("+1 minutes");
    $_SESSION['timer'] = $date;
}

if(isset($_SESSION['timer']) && !isset($_SESSION['already_asked']) && !isset($_COOKIE["customer_login"])){
    $date = new DateTime();
    if($_SESSION['timer'] <= $date){
        echo "<div id='popup'>";
        echo "<form class='mailing_list_form' method='POST' action='".$_SERVER['PHP_SELF']."'>";
        echo "<input type='hidden' id='mailing_list_hide' name='mailing_list_hide' value='hide'>";
        echo "<button id='mailing_list_close' name='mailing_list_close' onclick='this.form.submit();'>X</button>";
        echo "</form>";
        echo "<h2>SUBSCRIBE FOR LATEST DISCOUNTS & COMPETITIONS</h2>";
        echo "Stay up to date with our latest products";
        echo "<br /><br />";
        echo "<form class='mailing_list_form' method='POST' action='".$_SERVER['PHP_SELF']."'>";
        echo "<input type='text' id='mailing_list_input' name='mailing_list_input' placeholder='Enter your email address here'>";
        echo "<button id='mailing_list_button' name='mailing_list_button' onclick='this.form.submit();'>";
        echo "<img src='media/icons/subscribe_icon.png' alt='subscribe icon' width='15px' height='15px'>";
        echo "</button>";
        echo "</form>";
        if(isset($email_warning)){echo $email_warning;}
        echo "<p>By continuing you agree to the <u><a href='page/Privacy-and-Cookie-Policy'>Privacy and Cookie Policy</a></u>.</p>";
        echo "</div>";
    }
}
?>

2 个答案:

答案 0 :(得分:0)

PHP是一种服务器端语言,(不)活动发生在客户端。您必须在后者上实现解决方案,例如使用JS:

var popupTimer,
TIME_OUT = 60;

function displayPopup() {
    // display the popup
}

popupTimer = setTimeout(displayPopup, TIME_OUT);

$(document).on('click change keypress', function() {
    clearTimeout(popupTimer);

    // Reset
    popupTimer = setTimeout(displayPopup, TIME_OUT);
});

答案 1 :(得分:0)

这就是我最后想出的。如果他们正在网站上导航,它将获得会话计时器,如果页面上的停滞点没有点击就将使用JS计时器

<script>
    function displayPopup(){
        $("#popup").fadeIn("slow");
    }
    function hidePopup(){
        $("#popup").css("display", "none");
    }
</script>
<?php
if(isset($_POST['mailing_list_input']) && $_POST['mailing_list_input']!=""){
    $email = $_POST['mailing_list_input'];
    if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $params = [$email];
        $sql = "INSERT INTO mailing_list(email, date_time) VALUES (?,now())";
        $stmt = DB::run($sql,$params);

        $date = new DateTime();
        unset($_SESSION['timer']);
        $_SESSION['already_asked'] = $date;
    }else{
        $email_warning = "<div id='warning'>Email address is not valid</div>";
    }
}
if(isset($_POST['mailing_list_hide']) && $_POST['mailing_list_hide']=="hide"){
    $date = new DateTime();
    unset($_SESSION['timer']);
    $_SESSION['already_asked'] = $date;
}
if(!isset($_SESSION['already_asked'])){
    echo "<script>var popup_timer = setTimeout(displayPopup, 10000);</script>";
}
?>
<div id='popup'>
<form class='mailing_list_form' method='POST' action='<?php echo $_SERVER['REQUEST_URI']; ?>'>
<input type='hidden' id='mailing_list_hide' name='mailing_list_hide' value='hide'>
<button id='mailing_list_close' name='mailing_list_close' onclick='this.form.submit();'>X</button>
</form>
<h2>SUBSCRIBE FOR LATEST DISCOUNTS & COMPETITIONS</h2>
<p>Stay up to date with our latest products</p>
<form class='mailing_list_form' method='POST' action='<?php echo $_SERVER['REQUEST_URI']; ?>'>
<input type='text' id='mailing_list_input' name='mailing_list_input' placeholder='Enter your email address here'>
<button id='mailing_list_button' name='mailing_list_button' onclick='this.form.submit();'>
<img src='media/icons/subscribe_icon.png' alt='subscribe icon' width='15px' height='15px'>
</button>
</form>
<?php if(isset($email_warning)){echo $email_warning;} ?>
<p>By continuing you agree to the <u><a href='page/Privacy-and-Cookie-Policy'>Privacy and Cookie Policy</a></u>.</p>
</div>
<?php
if(!isset($_SESSION['timer']) && !isset($_SESSION['already_asked'])){
    $date = new DateTime();
    $date->modify("+20 seconds");
    $_SESSION['timer'] = $date;
    echo '<script>hidePopup();</script>';
}
if(isset($_SESSION['timer']) && !isset($_SESSION['already_asked']) && !isset($_COOKIE["customer_login"])){
    $date = new DateTime();
    if($_SESSION['timer'] <= $date){
        echo '<script>displayPopup();</script>';
    }else{
        echo '<script>hidePopup();</script>';
    }
}
if(isset($_SESSION['already_asked'])){
    echo '<script>hidePopup();</script>';
}
?>