为什么在重新加载而不是窗口加载后引导模式弹出窗口. 可能是什么原因?



我正在每个页面上设置引导模式窗口,背景上带有隐藏的滚动,以获取访问者详细信息以进行潜在客户生成,因为我们获得了足够的流量。

我正在使用标头进行所有编码.php每次都使用加载弹出窗口的窗口。 单击关闭或提交选项后,弹出窗口应隐藏。并且单击关闭或一次提交后,任何页面上都不会再出现弹出窗口。

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

会话安全吗? 还有什么其他选择?

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

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

经过研究,我已经做了一次窗口重新加载。 它在 URL 中给出了一些变量

在这里查看 : https://www.carlo.in/new-cars-test

请向我建议一些其他选择或为我提供解决方案。

会话将防止模式在客户端打开浏览器的剩余时间内或直到会话在服务器端过期/关闭(短期)。只要 cookie 未从其计算机中删除,cookie 就会阻止模态重新出现(长期)

以下是我如何使用引导模式和 cookie 来做到这一点:

  1. 检查 Cookie 是否已存在。
  2. 如果 cookie 不存在,请运行模式。
  3. 确认模式后,在客户端计算机上存储 Cookie。

标头.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();
}
}

如果用户已登录,我个人更喜欢使用用户名,但如果用户名不存储在会话中,您可以使用他们的 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();
}
}

索引.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... ->

页脚.php

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

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

最新更新