用于弹出窗口处理的Cookie

时间:2014-03-03 16:10:50

标签: php jquery cookies bpopup

我想创建一个网站,其中每个独特用户24小时出现一次弹出窗口。为此我使用bPopup和cookies。我已经尝试了很多东西,现在我在代码中“丢失”了。你能帮助我让它按预期工作吗?

代码:

<?php
if (!isset($_COOKIE["Seen"])){
if ($_COOKIE['Seen'] != 'true') {
setcookie('Seen', 'false');
}
}
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"> </script>
<script type="text/javascript" src="js/popup.js"> </script>
<LINK REL=StyleSheet HREF="style/style.css" TYPE="text/css" MEDIA=screen>


<html>
        <head>  </head>
        <body>
<!-- Element to pop up -->

<div <?php if(isset($_COOKIE["Seen"])) {
                    if ($_COOKIE['Seen'] == 'true') {echo 'style="all:none; visibility:hidden; display:none">';}
                    else {
                    echo ' id="element_to_pop_up">';
                    $value = 'true';
                    $expire = time()+60*60*24; 
                    setcookie('Seen', $value, $expire);
                    }
    }               
                     ?> 

    <a href="#"class="b-close" style="position:absolute; margin-top:5px; margin-left:550px;"><img src="./image/close.png"><a/>
    <iframe frameBorder="0" name="iFrame" width="600" height="500" src="welcome.php" scrolling="no"></iframe>

</div>

        </body>
    </html>

2 个答案:

答案 0 :(得分:0)

试试这段代码。如果设置了cookie,则无需检查两次,因为您在顶部设置了此值并设置为false。如果未设置或者(由于某种原因)它不是'true',则此代码将其设置为false。然后,在中途,它只需要检查它是否真实。

最好只为每个条件打开一个单独的开放div,否则它会变得非常混乱和快速真实。

<?php
if (!isset($_COOKIE['Seen']) || $_COOKIE['Seen'] != 'true') {
    setcookie('Seen', 'false');
}
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"> </script>
<script type="text/javascript" src="js/popup.js"> </script>
<LINK REL=StyleSheet HREF="style/style.css" TYPE="text/css" MEDIA=screen>


<html>
<head>  </head>
<body>
<!-- Element to pop up -->

<?php
if ($_COOKIE['Seen'] == 'true') {echo '<div style="all:none; visibility:hidden; display:none">';}
else {
    echo '<div id="element_to_pop_up">';
    $value = 'true';
    $expire = time()+60*60*24;
    setcookie('Seen', $value, $expire);
}
?>

<a href="#"class="b-close" style="position:absolute; margin-top:5px; margin-left:550px;"><img src="./image/close.png"><a/>
    <iframe frameBorder="0" name="iFrame" width="600" height="500" src="welcome.php" scrolling="no"></iframe>

    </div>

</body>
</html>

答案 1 :(得分:0)

如下:

if(!isset($_COOKIE['popup']))
{
    setcookie('popup', time());
    echo '<script>alert(\'Here is your daily cookie :)\');</script>';
}
else
{
    if((time() - $_COOKIE['popup']) > (60*60*24))
    {
        setcookie('popup', time());
        echo '<script>alert(\'I see you enjoy our cookies, thanks for returning :)\');</script>';        
    }
}