为什么 bootstrap 模式弹出后重新加载而不是 window 加载。可能是什么原因?

why bootstrap modal popup after reload instead of window load. what may be causes?

我正在每个页面上设置 Bootstrap 模态 window,在背景上隐藏滚动条以获取访问者详细信息以生成潜在客户,因为我们获得了足够的流量。

我正在使用 header.php 进行所有编码,每次都使用 window 加载弹出窗口。 单击关闭或提交选项后应隐藏弹出窗口。单击关闭或立即提交后,任何页面上都不再弹出 window。

我正在使用 session 在关闭或提交点击后隐藏弹出窗口。 这工作正常。

session 安全吗?还有其他选择吗?

我遇到了什么样的问题 - 重新加载后出现弹出窗口,而不是 window 加载时出现。

看起来像 cookie 问题,header 缓存问题。我不知道

经过研究,我已经 window 重新加载一次。它在 url

中给出了一些变量

在此处查看:https://www.carlo.in/new-cars-test

请给我一些其他选择或提供解决方案。

Session 将在客户端打开浏览器的剩余时间内或直到服务器端的 session 为 expired/closed(short-term).只要 cookie 没有从他们的计算机中删除 (long-term)

,cookie 就会阻止模态重新出现

以下是我使用 bootstrap 模态和 cookie 的方法:

  1. 检查 cookie 是否已经存在。
  2. 如果 cookie 不存在,运行 模式。
  3. 确认模态后,在客户端机器上存储一个 cookie。

header.php

  # check if "Got-It!" button has been pressed
  if (isset($_POST['btn_modal'])) {
    $nameofCookie = $_POST['btn_modal'] . '-' . $_SERVER['REMOTE_ADDR']; // store name of modal with client's IP for the cookie's name
    $cookieExp = (86400 * 365); // time in seconds for cookie to expire
    # check if this cookie exists already
    if (!array_key_exists($nameofCookie, $_COOKIE)) {
      setcookie($name, '_any_value_goes_here_to_store_in_cookie', $cookieExp, '/'); // create cookie
      header('Location: https://' . $_SERVER['HTTP_HOST'] . URI); // refresh the current page after they click "Got It!"
      die();
    }
  }

我个人更喜欢在用户登录时使用用户名,但如果不将用户名存储在 session 中,您可以使用他们的 IP,如上所示。这里是用户名,只有一行不同

  # check if "Got-It!" button has been pressed
  if (isset($_POST['btn_modal'])) {
    $nameofCookie = $_POST['btn_modal'] . '-' . $_SESSION['username']; // store name of modal with client's username for the cookie's name
    $cookieExp = (86400 * 365); // time in seconds for cookie to expire
    # check if this cookie exists already
    if (!array_key_exists($nameofCookie, $_COOKIE)) {
      setcookie($name, '_any_value_goes_here_to_store_in_cookie', $cookieExp, '/'); // create cookie
      header('Location: https://' . $_SERVER['HTTP_HOST'] . URI); // refresh the current page after they click "Got It!"
      die();
    }
  }

index.php(或模式正在进行的任何页面)

<!-- code... -->

  <!-- begin: What's New? [Modal] -->
    <div class="modal fade" id="newModal" tabindex="-1" role="dialog">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title">What's new</h5>
          </div>
          <div class="modal-body">
            <ul>
              <li>NEW THIS</li>
              <li>NEW THAT</li>
              <li>NEW EVERYTHING</li>
            </ul>
          </div>
          <div class="modal-footer">
            <form method="post" action="<?=$_SERVER['REQUEST_URI'];?>">
              <button type="submit" class="btn btn-primary" name="btn_modal" value="newModal">Got It!</button>
            </form>
          </div>
        </div>
      </div>
    </div>
  <!-- end: What's New? [Modal] -->

<!-- more code... ->

footer.php

  if(array_key_exists($nameofCookie, $_COOKIE)) {
    echo "<script>
            $(window).on('load',function(){
              $('#newModal').modal('show');
            });
          </script>";
  }

设置cookie的脚本必须放在header中,最好是检查脚本和运行模态框放在html代码之后的任意位置对于模式,最好是页脚。