[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP Cookie expiry

Posted on 2012-08-23
11
Medium Priority
?
487 Views
Last Modified: 2012-08-23
I set a cookie with an expiration of 1 hour.
Is there a simple way to reset this when the visitor visits another page (within the hour).
I cannot use a empty time and rely on browser session because of certain reasons.
0
Comment
Question by:Gary
  • 3
  • 2
  • 2
  • +2
11 Comments
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 2000 total points
ID: 38327515
What does your code look like for setting the cookie?

If you set another cookie with the same name, it should overwrite the last one, so just keep setting it for an hour out...
0
 
LVL 58

Author Comment

by:Gary
ID: 38327572
Thats what I assumed, just wasn't sure if there was anything general in PHP that might reset them all in one go.

setcookie("tncad",$title,time() + 3600, "/");
0
 
LVL 9

Expert Comment

by:Erdinç Güngör Çorbacı
ID: 38327576
What is the exact problem ? Are cookies deleted after 1 hour although you browse page within less than 59 minutes ?

As gr8gonzo indicated setting cookie again will set new 1 hour for same cookie.

I think you are not setting the cookie in your all pages. Try including setter to all pages
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 58

Author Comment

by:Gary
ID: 38327588
Only called on one page - and there is only one page.
But the cookie is only set once on first visit - after that it's only read.
But the worry is after one hour it is deleted.

I don't know if they are deleted yet, will have to wait for a situation where it might happen.
0
 
LVL 12

Expert Comment

by:zappafan2k2
ID: 38327681
It sounds like you are using the following type of logic:
if (isset($_COOKIE['tncad'])) {
    // do something
} else {
    setcookie("tncad",$title,time() + 3600, "/");
    // do something else
}

Open in new window


If there is only one page, then why not just execute the setcookie statement outside of the if statement; e.g.:
if (isset($_COOKIE['tncad'])) {
    // do something
} else {
    // do something else
}
setcookie("tncad",$title,time() + 3600, "/");

Open in new window

0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 38327685
zappa, that was already suggested twice.
0
 
LVL 12

Expert Comment

by:zappafan2k2
ID: 38327699
Sorry gr8gonzo - no disrespect intended.  I was mostly trying to illustrate that it be done outside the if/then/else block, which hadn't been explicitly stated.

I'll be more careful from now on.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38327701
Just use...
setcookie("tncad",$title,time() - 3600, "/"); 

Open in new window

That sets the time to an hour before which makes it expire immediately.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38327706
Maybe I misunderstood you.  Are you trying to keep the cookie 'set'?  You know you can read it and set it in the same page?
0
 
LVL 9

Expert Comment

by:Erdinç Güngör Çorbacı
ID: 38327708
a simple logic : if you want to refresh timeout for a cookie you need to do the  
refresh
action , so for the third :D and the last time ..... please keep setting cookie for every load.

and btw you don't need to wait :) .... just clear browsers cookie cache and do your tests with 1 minute than you can change to 1 hour
0
 
LVL 58

Author Closing Comment

by:Gary
ID: 38327875
I want to keep it set at one hour, so if someone revisits the page within one of hour of the first visit then the 'timeout' is reset
So I'm happy to accept gr8gonzo's answer unless someone has an issue with it.
@ zappafan2k2
That is the logic but gr8gonzo gave the solution to just resetting it.

Refresh wouldn't be an issue but since the cookie is already set it is the same as visiting a new page and the timeout is still occuring from the time of the first visit.
I just needed to clarify it in my own mind how it works.
There is multiple cookie values and I just thought maybe there was an easy way in php to reset them all in one go, obviously not and I'm happy with resetting timeout for all the values.

Thanks all for your comments
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month18 days, 8 hours left to enroll

826 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question