How to show a certain div only once on the site?

What is: javaScript\jQuery, html\css, php.

I need to show a window (div block) once. The user can close it.

How to make this window (div block) no longer shown the next time you visit the site, and when you go to other pages?

Clarification: the window is shown depending on the result of the php function.

UPD.

May be considered closed.

Many thanks to Kremchik, the last option works as it should.

UPD.

Google chrome 14.0.835.35 beta-m may have strange cookies


Answer 1, authority 100%

At the beginning of any page write:

$first_time = false;
if (!isset($_COOKIE[""])) {
    $first_time = true;
    setcookie("", "_", time() + 3600*24*10, "/");
    /*   ,       */
}

Where the div is rendered:

if ($first_time) {
    my_echo_div();//  
}

It is very important that setcookie()should be called exactly at the beginning of the page, i.e. to any conclusion. If there is a line break first, and then the opening <?phptag, then the cookie will not be set.

You should probably also specify what to do if you want to reset cookies:

setcookie("", "", 0, "/");//   ,        70- :)

PSYou indicated that you also have php, but this whole thing can be solved with js: one of many articles

PSIf you want to be able to close a div, ie. show it until the user closes it, then a possible solution is to set the cookie on close with javascript. That is, remove the setcookie () line from the php code and transfer its analogue to js on the page. The full version might look like this:

<?php
    $first_time = false;
    if (!isset($_COOKIE["olole"])) {
        $first_time = true;
    }
    function my_echo_div() {
        echo '<a href="#" onclick="confirm()" id="noob">New member!</a><span id="old" style="display: none">Old member :)</span>';
    }
?>
<?php if($first_time) my_echo_div(); else echo "Old member :)" ?>
<script type="text/javascript">
function setCookie (name, value, seconds) {
    if (typeof(seconds) != 'undefined') {
        var date = new Date();
        date.setTime(date.getTime() + (seconds) * 1000);
        var expires = "; expires=" + date.toGMTString();
    }
    else {
        var expires = "";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}
function confirm() {
    setCookie("olole", "ololo", 10);
    document.getElementById("noob").style.display = "none";
    document.getElementById("old").style.display = "";
}
</script>

Answer 2, authority 25%

Cookies are nice if it’s not an authorized user, but if the output is for registered and authorized users, then it’s better to keep control in the database.