【问题标题】:Creating a popup after a user has been on the site after a specific period of time在用户在特定时间段后访问网站后创建弹出窗口
【发布时间】:2018-11-29 14:45:59
【问题描述】:

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

通过在我的所有页面中包含此 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>";
    }
}
?>

【问题讨论】:

  • 为此,您需要 JavaScript 和对 setTimeout() 的调用,然后运行一些显示额外内容的 JS 代码。不过就个人而言,我讨厌这样的弹出窗口,尤其是当它们覆盖整个屏幕并阻止我做我正在做的事情时。如果它在角落里弹出很小,没有阻挡其他任何东西,那么很好,但如果它妨碍了我,它真的让我很恼火,让我无法使用该网站。如果网站所有者不费心使用任何方法来检测他们是否已经向我展示了相同的弹出窗口 10 次,则更是如此。
  • @ADyson 感谢您的评论,是的,我曾想过在 JS 中执行此操作,但我希望计时器在页面之间保持不变,而不是每次进入新页面时都重新启动计时器并且'不知道我怎么能做到这一点
  • 这更难。您也许可以使用手动倒计时并将其保存在 sessionStorage 每次他们离开页面时,然后在每次页面加载时获取它。我当然不会尝试在服务器端做任何事情。并且可能使用 cookie 来查看用户之前是否看过它,或者多次关闭它,或者已经注册。
  • @ADyson,是的,我也讨厌他们,但不幸的是,我只是一个卑微的程序员,正在做我被告知的事情。但它肯定会在底角尽可能小
  • 您可以使用 ajax(和 JS)并检查存储日期/时间。我当然在这里大声思考。

标签: php mysql datetime session


【解决方案1】:

PHP 是一种服务器端语言,(in)activity 发生在客户端。您必须在后者上实施您的解决方案,例如使用 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);
});

【讨论】:

  • 我不认为这对我有用,因为希望用户会处于活动状态,并且我不希望它在一定程度的不活动后触发。我希望他们在网站上停留特定时间后触发它,无论他们做了什么活动,比如点击不同的页面,使用此解决方案会重置计时器。
【解决方案2】:

所以这就是我最终想出的。如果他们正在浏览网站,它将获得会话计时器,如果页面上的停顿而不点击它只会使用 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>';
}
?>

【讨论】:

    猜你喜欢
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多