弹出窗口仅显示在网站的第一页上


Popup showing only on the first page of the website

我有一个用php构建的网站,我在index.php文件中添加了一个弹出框。

我在 body 标签中有一个 onload 方法,该方法在页面加载时显示弹出窗口,但它会在网站的每个页面上触发。

我试图添加一个条件:

if (document.URL == "mymainpage")

但是似乎当我转到下一页时,它被触发了,当我想关闭弹出窗口时,它会再次检查 url 并更改,因此它不会关闭弹出窗口。

仅在用户第一次访问网站时显示弹出窗口的最佳方法是什么?

PS:我在索引.php文件中使用此代码:

<body<?php echo $body_attr;?>>
<?php
session_start();
$body_attr = "";
if(!isset($_SESSION["popup_shown"]))
{
    $body_attr = " onload='"show('popupContainer')'"";
    $_SESSION["popup_shown"] = true;
}
?>
<script>
function show(id) {
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
    }
</script>
<!-- ============== THE POPUP ======================= -->
<div id="popupContainer" style="display:block; position:fixed; top:0; left:0; background-color:rgba(0,0,0,0.7); height:100%; width:100%; z-index:999;">
    <div id="popup" style="border:2px solid white; font-size:20px; line-height:25px; text-align:center; width:75%; margin:0 auto; background-color:#18af8e; position:relative; top:35%; padding:15px; padding-bottom:0px; border-radius:20px;">
        <p style="color:white;">Din cauza perioadei foarte aglomerate si a vacantei de sarbatori, comenzile primite dupa <b>20 Decembrie</b> vor fi onorate in ianuarie. 
<br></br>
Va multumim pentru intelegere si va dorim <b>Sarbatori Fericite!</b></p>
        <button onclick="show('popupContainer')" style="display:inline; position:absolute; top:-30px; left:100%; padding:0 5px 2px 5px; border-radius:50%; border:2px solid black; background:none; font-weight:bold">x</button>
    </div>
</div>

为什么不在 PHP 中使用 cookie?

$popup = "<div class='popup'>This popup is awesome</div>";
if($_COOKIE["PopupShown"] != true){
    echo $popup;
}
setcookie("PopupShown", true);

例如。。

更多信息在这里: http://php.net/manual/en/function.setcookie.php

session_start();
if(!isset($_SESSION["popup_shown"]))
{
    echo "//javascript and/or html code to show the popup";
    $_SESSION["popup_shown"] = true;
}


更新:根据您的评论,只需更改<body>标签中的内容,然后:

session_start();
$body_attr = "";
if(!isset($_SESSION["popup_shown"]))
{
    $body_attr = " onload='"show('divid')'"";
    $_SESSION["popup_shown"] = true;
}

在您的 HTML 中:

<body<?php echo $body_attr;?>>


注意:在发生任何其他输出之前,必须调用session_start()