swfcfan4 Posted December 1, 2009 Share Posted December 1, 2009 Hi everyone. I have a php program which uses a session cookie to check if a user of my website has voted more than once. The problem is, after the deadline, of say a time limit of a week to vote, I need to display the result of the vote and I am having trouble setting a time limit on the session cookie to expire after a week so I can display the vote count. I am aware of the function ini_set("session.gc_maxlifetime", "TIME LIMIT HERE"); but am unsure how I can use this to make the session cookie display a vote count after the deadline has passed. Any help would be good. And I thank you in advance if you can help solve my problem. Quote Link to comment https://forums.phpfreaks.com/topic/183553-a-sesion-cookie-expiration-time/ Share on other sites More sharing options...
Daniel0 Posted December 1, 2009 Share Posted December 1, 2009 Why do you need it to expire? You can just do something like this: if ($expired) { // show results } else if ($hasNotVoted) { // show voting controls } else { // tell the user they've already voted } Quote Link to comment https://forums.phpfreaks.com/topic/183553-a-sesion-cookie-expiration-time/#findComment-968807 Share on other sites More sharing options...
swfcfan4 Posted December 1, 2009 Author Share Posted December 1, 2009 Well I need a time limit of a week so that people can vote and not see the results until after a week. I am interested to know what the $expired does though if you could tell me. Is it defined already in php or would I need to declare it? Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/183553-a-sesion-cookie-expiration-time/#findComment-968810 Share on other sites More sharing options...
Daniel0 Posted December 1, 2009 Share Posted December 1, 2009 Well I need a time limit of a week so that people can vote and not see the results until after a week. A week after they voted, or a week after the poll started? If it's after they voted, just store the time they voted in a session as well. If it's after the poll started, just store start time (e.g. using time) and check against that instead. Either way, if we say that you have your timestamp as $start, you can check if one week has passed by doing if (strtotime('+1 week', $start) >= time()). I am interested to know what the $expired does though if you could tell me. Is it defined already in php or would I need to declare it? It was just meant as a placeholder for a boolean value determining whether or not the time limit has passed. It was just pseudocode to convey the logic that is required to make it work. Quote Link to comment https://forums.phpfreaks.com/topic/183553-a-sesion-cookie-expiration-time/#findComment-968818 Share on other sites More sharing options...
swfcfan4 Posted December 1, 2009 Author Share Posted December 1, 2009 It is after a week that the poll started. I have the following in my code already: session_start (); $_SESSION["previousVisit"] = time(); if ($_SESSION["previousVisit"]) { // tell the user they have already voted } else { // take their vote from html page from $_POST // dsiplay message to user with their selection } would I need to put yuor if statement above my first if or put it in the else part? Thanks for the help you have given me so far. This is the only thing that has been bothering me for a couple of days. Quote Link to comment https://forums.phpfreaks.com/topic/183553-a-sesion-cookie-expiration-time/#findComment-968823 Share on other sites More sharing options...
Daniel0 Posted December 1, 2009 Share Posted December 1, 2009 It's easy enough if it's a week after the poll creation. How are you storing the poll data, in a database? Just store the timestamp of the creation time along with it. You can then use logic like in my previous post to determine whether or not the poll has expired and thus whether or not the results should be displayed to the user. Quote Link to comment https://forums.phpfreaks.com/topic/183553-a-sesion-cookie-expiration-time/#findComment-968826 Share on other sites More sharing options...
swfcfan4 Posted December 1, 2009 Author Share Posted December 1, 2009 I am storing my results in a text file. So would what you said in the post above mine work still? Quote Link to comment https://forums.phpfreaks.com/topic/183553-a-sesion-cookie-expiration-time/#findComment-968830 Share on other sites More sharing options...
Daniel0 Posted December 1, 2009 Share Posted December 1, 2009 As long as you somehow store the time it shouldn't be a problem. I would recommend using a real database though. It's way easier managing data if you do that. Quote Link to comment https://forums.phpfreaks.com/topic/183553-a-sesion-cookie-expiration-time/#findComment-968831 Share on other sites More sharing options...
swfcfan4 Posted December 1, 2009 Author Share Posted December 1, 2009 Ok thanks, I'm just a little unsure about the timestamp though, do i need to create one first? is it possible for you to give me an example? Quote Link to comment https://forums.phpfreaks.com/topic/183553-a-sesion-cookie-expiration-time/#findComment-968838 Share on other sites More sharing options...
Daniel0 Posted December 1, 2009 Share Posted December 1, 2009 The function time can give you a timestamp. It will be fine for this. Quote Link to comment https://forums.phpfreaks.com/topic/183553-a-sesion-cookie-expiration-time/#findComment-968840 Share on other sites More sharing options...
swfcfan4 Posted December 1, 2009 Author Share Posted December 1, 2009 OK so I currently have this in my code updated with what you gave me: session_start (); $_SESSION["previousVisit"] = time(); if ($_SESSION["previousVisit"]) { // tell the user they have already voted } else if (strtotime('+1 week', $_SESSION["PreviousVisit"]) >= time()) { // display vote result } else { // take their vote from html page from $_POST // dsiplay message to user with their selection } Is this correct? I feel like its a little wrong. Hope you can guide me in the right direction. Quote Link to comment https://forums.phpfreaks.com/topic/183553-a-sesion-cookie-expiration-time/#findComment-968847 Share on other sites More sharing options...
Daniel0 Posted December 1, 2009 Share Posted December 1, 2009 This: strtotime('+1 week', $_SESSION["PreviousVisit"]) >= time() will always be true because you set $_SESSION["PreviousVisit"] = time(); It's always true that one week in the future is greater than right now when you store the timestamps in Unix format. You need to compare against the time your poll started. This is static. Also, that statement is used to check if the poll is still running. You would want strtotime('+1 week', $startTime) < time() instead to check if it has expired. Quote Link to comment https://forums.phpfreaks.com/topic/183553-a-sesion-cookie-expiration-time/#findComment-968850 Share on other sites More sharing options...
swfcfan4 Posted December 1, 2009 Author Share Posted December 1, 2009 So I need to start the session then create a variable called $start which would be the start time of the poll e.g. start_session (); $start = time() Then the session variable I had e.g. $_SESSION["previousVisit"] = time() and then compare these in my If statement? Quote Link to comment https://forums.phpfreaks.com/topic/183553-a-sesion-cookie-expiration-time/#findComment-968855 Share on other sites More sharing options...
swfcfan4 Posted December 1, 2009 Author Share Posted December 1, 2009 Ok so I have had another go on this using dates instead and have come up with: if ($_SESSION["previousVisit"]) { // tell user they have already voted and cannot vote again } else if ( date("d/m/Y") < $finish ) { // take the vote from html web form using $_POST // thank user for voting and display their choice } else { // tell user voting has finished and that their vote will not count // display the result of the vote to the user } $finish = mktime(11, 15, 0, 12, 2, 2009); $_SESSION["previousVisit"] = time(); the problem is even if the finish is set to the 2nd December, the user is getting the message that its too late to vote. Any ideas anyone of whats wrong? Quote Link to comment https://forums.phpfreaks.com/topic/183553-a-sesion-cookie-expiration-time/#findComment-969033 Share on other sites More sharing options...
swfcfan4 Posted December 1, 2009 Author Share Posted December 1, 2009 I really hope that someone can help me. (Sorry to post again) Quote Link to comment https://forums.phpfreaks.com/topic/183553-a-sesion-cookie-expiration-time/#findComment-969157 Share on other sites More sharing options...
premiso Posted December 1, 2009 Share Posted December 1, 2009 The $finish should be before the if statement. As it stands right now, $finish will default to 0 and so so date will never really be less than it. Quote Link to comment https://forums.phpfreaks.com/topic/183553-a-sesion-cookie-expiration-time/#findComment-969175 Share on other sites More sharing options...
swfcfan4 Posted December 1, 2009 Author Share Posted December 1, 2009 To test to see if it works, I have changed the mktime to 30th November 2009, and I tried what you said and still it doesn't work. Why is it going wrong? Quote Link to comment https://forums.phpfreaks.com/topic/183553-a-sesion-cookie-expiration-time/#findComment-969195 Share on other sites More sharing options...
premiso Posted December 1, 2009 Share Posted December 1, 2009 date("d/m/Y") That will test d/m/Y vs a unix timestamp, if you are just checking todays date use time instead. Quote Link to comment https://forums.phpfreaks.com/topic/183553-a-sesion-cookie-expiration-time/#findComment-969206 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.