Jump to content

Recommended Posts

Hi all,

 

I'm struggling with a program I'm trying to write with cookies, so any help would be very much appreciated!!

 

What I'm wanting to do is when someone visits my site, I want to display content until the end of the day, and once the days up it disappears forever/until the cookies are deleted.

 

Could someone help me with this?

 

Thanks lots in advance,

 

Jake

Link to comment
https://forums.phpfreaks.com/topic/235496-cookie-help/
Share on other sites

You may want something like this:

 


$midnighthour = "23";
$midnightmin = "59";
$hour = date("G");
$hoursminutes = $hour*60;
$minutes = date("i");
$minutesfrommidnighta = $hoursminutes+$minutes;
$minutesfrommidnightb = 1440-$minutesfrommidnighta;

$username = $_GET['username'];

$getcookie = $_COOKIE['user'];
if($getcookie==0) {
$createcookie = setcookie("user",$username,time()+$minutesfrommidnightb);
}
else {
if($getcookie==$username) {
// the code you want to execute if the user cookie is there
}
else {
// the code you want to execute if the user cookie does not exist.
}
}

Link to comment
https://forums.phpfreaks.com/topic/235496-cookie-help/#findComment-1210818
Share on other sites

I only want certain content to appear on a page until midnight the first time they visit, after that it never appears again until the cookie is deleted.

 

 

You could create a cookie that stores the timestamp of their first visit. Using PHP, you could then compare the cookie timestamp to your midnight deadline. If the cookie timestamp is less than the midnight deadline timestamp, show them the data.

 

mktime() could be used to create the midnight deadline timestamp:

http://php.net/manual/en/function.mktime.php

 

time() could be used to get the initial timestamp of the cookie:

http://php.net/manual/en/function.time.php

 

setcookie() could be used to create the cookie:

http://php.net/manual/en/function.setcookie.php

 

$_COOKIE could be used to read the cookie:

http://php.net/manual/en/reserved.variables.cookies.php

 

Link to comment
https://forums.phpfreaks.com/topic/235496-cookie-help/#findComment-1210880
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.