Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/183553-a-sesion-cookie-expiration-time/
Share on other sites

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.

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.

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.

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.

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.

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.

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?

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?

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.