Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-12-2013, 09:14 PM   PM User | #1
peteyshivery
New Coder

 
Join Date: Dec 2011
Posts: 11
Thanks: 5
Thanked 0 Times in 0 Posts
peteyshivery is an unknown quantity at this point
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> 
peteyshivery is offline   Reply With Quote
Old 01-12-2013, 09:17 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 01-12-2013, 09:19 PM   PM User | #3
peteyshivery
New Coder

 
Join Date: Dec 2011
Posts: 11
Thanks: 5
Thanked 0 Times in 0 Posts
peteyshivery is an unknown quantity at this point
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.
peteyshivery is offline   Reply With Quote
Old 01-12-2013, 09:32 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
peteyshivery (01-12-2013)
Old 01-12-2013, 09:45 PM   PM User | #5
peteyshivery
New Coder

 
Join Date: Dec 2011
Posts: 11
Thanks: 5
Thanked 0 Times in 0 Posts
peteyshivery is an unknown quantity at this point
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?
peteyshivery is offline   Reply With Quote
Old 01-12-2013, 09:49 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
peteyshivery (01-12-2013)
Old 01-12-2013, 09:52 PM   PM User | #7
peteyshivery
New Coder

 
Join Date: Dec 2011
Posts: 11
Thanks: 5
Thanked 0 Times in 0 Posts
peteyshivery is an unknown quantity at this point
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!
peteyshivery is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:38 PM.


Advertisement
Log in to turn off these ads.