Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: IF Visited

  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    11
    Thanks
    5
    Thanked 0 Times in 0 Posts

    IF Visited

    Hello guys,

    You have all been a great help every time I come on here. For those who remember me, I am really fuzzy with PHP. In fact, I barely know it!

    But I have a question - I have included a code below. Basically, using a cookie, if a user has visited in the past two hours, I do not want to show the following code below for the user. It is a pop-up, so basically, I want users on their first time to see the pop-up, then every two hours consecutively.

    Here is the code below, was wondering if anyone is familiar with cookie PHP coding to help? I really appreciate it in advance!

    PHP Code:
            <script type="text/javascript" src="/lightbox/js/lightbox/jquery.lightbox.min.js"></script>
      <script type="text/javascript">
        jQuery(document).ready(function($){
          $.lightbox("/images/popup.png");
        });
      </script> 

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    I don't see where PHP would be involved here at all. Since you're already requiring JS to be in use, just use JS' cookie capability to do this. There's no reason to let PHP become involved here at all.

  • #3
    New Coder
    Join Date
    Dec 2011
    Posts
    11
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    I don't see where PHP would be involved here at all. Since you're already requiring JS to be in use, just use JS' cookie capability to do this. There's no reason to let PHP become involved here at all.
    Again, I'm not too familiar with either. Couldn't you use an "IF" statement and go and do something like If user has visited in the last two hours, show nothing.

    And if a user has, show the script above?

    That's basically the only knowledge I have of PHP haha.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You can do that in PHP, I just don't know why you wouldn't use the JS anyway since its required to show the code.
    PHP Code:
    <?php

    $iDuration 
    5;

    $bShowIt = !isset($_COOKIE['lastShown']) || isset($_COOKIE['lastShown']) && (time() - $_COOKIE['lastShown']) > $iDuration true false;

    if (
    $bShowIt)
    {
        
    setcookie('lastShown'time(), time() + $iDuration);
        
    // do whatever you gotta do to show it
        
    printf('Show something every %d seconds'$iDuration);
    }
    Looks like it'd do the trick. Server languages tend to limit their reliance on cookies as much as they can.

  • Users who have thanked Fou-Lu for this post:

    peteyshivery (01-12-2013)

  • #5
    New Coder
    Join Date
    Dec 2011
    Posts
    11
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    You can do that in PHP, I just don't know why you wouldn't use the JS anyway since its required to show the code.
    PHP Code:
    <?php

    $iDuration 
    5;

    $bShowIt = !isset($_COOKIE['lastShown']) || isset($_COOKIE['lastShown']) && (time() - $_COOKIE['lastShown']) > $iDuration true false;

    if (
    $bShowIt)
    {
        
    setcookie('lastShown'time(), time() + $iDuration);
        
    // do whatever you gotta do to show it
        
    printf('Show something every %d seconds'$iDuration);
    }
    Looks like it'd do the trick. Server languages tend to limit their reliance on cookies as much as they can.
    Fou, thank you so much for this, it works great! The iDuration is per seconds right?

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by peteyshivery View Post
    Fou, thank you so much for this, it works great! The iDuration is per seconds right?
    That's correct yes. Cookies always work in integer seconds, and I just extended it to work with a value that's also an integer.

  • Users who have thanked Fou-Lu for this post:

    peteyshivery (01-12-2013)

  • #7
    New Coder
    Join Date
    Dec 2011
    Posts
    11
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    That's correct yes. Cookies always work in integer seconds, and I just extended it to work with a value that's also an integer.
    Thanks again Fou-Lu it works definitely a charm. I thanked your post and if there's anything else I can do, please let me know!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •