I am trying to make a countdown timer for a game. And the problem is, when the user move to another page, then countdown cannot continue the remaining time. I am trying to set a cookie and session, but I have no idea about it.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Cookies are sent back and forth with every HTTP request, hence should be avoided for storing unnecessary variables.
To maintain the timer on the server-side, a session is the best choice. It will take care of the cookie business automatically, creating only the bare-minimum cookie overhead to keep track of the session. Session variables live on the server, and are connected to HTTP requests by unique cookies.
Regarding your countdown timer: Since you cannot keep a function running on the server to keep flipping the bits, the best bet is to keep track of the end time.
To initiate a session in PHP, use
session_start(). Then store your values in$_SESSION[]array, and they’ll persist across requests. Here’s an example: