Question

I would like to make a site where a popup appears once in 24h for every unique user. For this I use bPopup and cookies. I have tried lot of things and for now I am kind of "lost" in the code. Could you help me to make it work how it is supposed to be?

The code:

<?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>
Était-ce utile?

La solution

What about something like:

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>';        
    }
}

Autres conseils

Try this code. You don't need to check twice if the cookie is set since you're accounting for this at the top and setting to false. This code will set it to false if it is not set OR if (for some reason) it is not 'true'. Then, half way down, it will only need to check if it is true or not.

It's also best to just have a separate opening div open for each conditional, otherwise it can get very confusing and sloppy real fast.

<?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>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top