CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   IF Visited (http://www.codingforums.com/showthread.php?t=285689)

peteyshivery 01-12-2013 09:14 PM

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> 


Fou-Lu 01-12-2013 09:17 PM

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.

peteyshivery 01-12-2013 09:19 PM

Quote:

Originally Posted by Fou-Lu (Post 1305822)
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.

Fou-Lu 01-12-2013 09:32 PM

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.

peteyshivery 01-12-2013 09:45 PM

Quote:

Originally Posted by Fou-Lu (Post 1305829)
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?

Fou-Lu 01-12-2013 09:49 PM

Quote:

Originally Posted by peteyshivery (Post 1305834)
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.

peteyshivery 01-12-2013 09:52 PM

Quote:

Originally Posted by Fou-Lu (Post 1305835)
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!


All times are GMT +1. The time now is 01:42 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.