1

How would it be done with this? I have jQuery if that would help.

<div id="RLAD-wrapper">
    <div id="RLAD">
        <p>stuff</p>
    </div>
</div>
  • What exactly do you mean? What is "a user session"? From what I can tell, you want to write out a session with PHP on the same page where this `
    ` is displayed. Then, if session variable exists, don't show. As soon as they leave your website, the session is detroyed and it will show again the next time they visit.
    – icecub Dec 09 '16 at 22:43
  • @icecub Well what do you recommend? I just don't want users to feel it's forced on them every time. Maybe every three days? – Derrick Stewart Dec 09 '16 at 22:53
  • @DerrickStewart when the user lands on the page, create a cookie once the page has been displayed, then just simply check that cookie to see if you should show the message or not http://stackoverflow.com/questions/1458724/how-do-i-set-unset-cookie-with-jquery – Canvas Dec 10 '16 at 01:31
  • @DerrickStewart - What do you want to do exactly? \Allow this div to be shown only at the session start but not when the client goes back to it? – Bekim Bacaj Dec 10 '16 at 03:02
  • It depends on what exactly the `
    ` contains. Obviously if it contains important information, you don't want to wait that long to show it again. On the other hand, if it's a reminder to do something that isn't important to your visitors: Say a facebook upvote for example, you don't want it to become annoying. Otherwise you lose visitors. Do ppl visitor your website every day? Do they visit it multiple times a day? If yes, then 3 days is acceptable. If not, you might want to increase it up to 7 days depending on how often ppl visit your website. A cookie would be the way to go with this btw
    – icecub Dec 10 '16 at 05:15

3 Answers3

2
if(localStorage.getItem("iknowyou")) {
  document.body.innerHTML = "You were already here";
} else {
  document.body.innerHTML = "Oh. A new guest...";
  localStorage.setItem("iknowyou", "true");
}

This utilizes localStorage to store a persistent state across sessions.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
1

You could also do it with cookies:

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

</head>
<body>
<div class="mydiv" style="display: none;">
    this is a div
</div>
<script
        src="https://code.jquery.com/jquery-3.1.1.slim.min.js"
        integrity="sha256-/SIrNqv8h6QGKDuNoLGA4iret+kyesCkHGzVUUV0shc="
        crossorigin="anonymous"></script>
<script>
    $(function() {
        // Cookies
        function setCookie(name, value, days) {
            if (days) {
                var date = new Date();
                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                var expires = "; expires=" + date.toGMTString();
            }
            else var expires = "";

            document.cookie = name + "=" + value + expires + "; path=/";
        }

        function getCookie(name) {
            var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == ' ') c = c.substring(1, c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
            }
            return null;
        }

//        Validate cookie
        var myCookie = getCookie("MyCookie");
        if (myCookie == null) {
//                alert('No cookei');
            $('.mydiv').css('display','block');
            setCookie("MyCookie", "foo", 7);
        }
        else {
//                alert('yes cookei');
            $('.mydiv').css('display','none');
        }
    });
</script>
</body>
</html>
0

The code below sets an item in localStorage to Date.now, and checks if it is past 3 days. The setting of the item is in an else statement to prevent the user from getting their time reset every single time they run the website.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Put anything you want here</title>
    </head>
    <body>
        <div id="aDiv">
            Div content
        </div>
        <script>
            if(Date.now() - parseInt(localStorage.getItem("pageVisitedTime").getTime(), 10) < 2.592e+8){
                document.getElementById("aDiv").style.display = "none";
            }
            else{
                localStorage.setItem("pageVisitedTime", "" + Date.now());
            }
        </script>
    </body>
</html>
MasterBob
  • 550
  • 1
  • 8
  • 19
  • that's not what "user session" means in the browser world. – Bekim Bacaj Dec 10 '16 at 02:57
  • @bekim it is not, then what is it? It is specified in the comments, "every three days". Now if "user session" is really what it means, that means to use `sessionStorage`. **But it doesn't mean that!!!** So, there comes this, my code. – MasterBob Dec 10 '16 at 03:18
  • Well, in that case the op needs to correct his question with what he really means. – Bekim Bacaj Dec 10 '16 at 03:22
  • @BekimBacaj What a user session means is debatable. Some would say it's from the moment a visitor enters the website till the moment they leave. Others would say it's from the moment they login till they logout again, but only during that single visit. Otherwise it doesn't count as a session. The key is `user`. This can refer to the user of the website or a member of it. Hence my first comment to the question itself: `What is "a user session"?` – icecub Dec 10 '16 at 05:27
  • I think the OP needs to decide what is a "user session" here. I would update my answer if the OP was more specific on this particular area. Although, I'm assuming that it means by regular time, not affected by anything else, because 3 days is a bit too much for those two options as stated above. – MasterBob Dec 10 '16 at 06:25
  • @icecub there's substantial difference between a "user session" and a "browser session" and there has been nothing debatable about them ever since www existence. Browsing session counts from the first landing on a certain domain, throughout the site visit and it ends when the client exits the the site e.g the domain. Whereas 'user session' is a bit more specific, and it counts only from the user login until the user logs out. And since you happen to know the difference, your question should have been: what do you mean by it, as it seems that the op is new in the business. – Bekim Bacaj Dec 10 '16 at 06:54
  • @BekimBacaj As you can read clearly in my comment `What exactly do you mean? What is "a user session"?` I agree, perhaps I should've worded that differently, but it's exactly what I've asked ;) You can also clearly see OP clerifying the question after that with the question `Maybe every three days?`, which means he isn't talking about sessions at all. What he needs is a cookie instead. Or what this answer is talking about. LocalStorage. – icecub Dec 10 '16 at 07:08
  • I suggested an edit to change the way the OP asked this question, so that issue is resolved. – MasterBob Dec 10 '16 at 16:14
  • Actually, never mind, it was rejected. – MasterBob Dec 10 '16 at 23:11