Time in a cookie.

F

fb

Hi everyone. I'm planning to store the current date and time in a
cookie and then compare, the time in the cookie to the current time. If
30 minutes elapses, then I will ... I dunno ... redirect to another
page I guess.

My question is ... How do I store the current date and time in a
cookie? If you can think of other ways of monitoring the time, so that
I can countdown properly even with a page refresh, let me know...

Thanks.
 
J

Joost Diepenmaat

fb said:
Hi everyone. I'm planning to store the current date and time in a
cookie and then compare, the time in the cookie to the current time.
If 30 minutes elapses, then I will ... I dunno ... redirect to another
page I guess.

Unless you're proposing to redirect people off your site after half an
hour of actively browsing it (and why not, I'm sure they'd come back),
you've not thought this through.
 
F

fb

Joost said:
Unless you're proposing to redirect people off your site after half an
hour of actively browsing it (and why not, I'm sure they'd come back),
you've not thought this through.
Hmmm. I've worded it wrong...funny though. It's for answering
questions. A user has 20 minutes to answer some questions, they either
complete the questions, or if they are out of time, they are kicked to
the final mark page.

I've decided to place the current time in a php _SESSION variable and
then just use Javascript to compare the time in the _SESSION to the
current time. There is a problem through...

Is it possible to read a php session variable from javascript? Maybe I
should start a new thread. :)
 
J

Joost Diepenmaat

fb said:
Hmmm. I've worded it wrong...funny though. It's for answering
questions. A user has 20 minutes to answer some questions, they
either complete the questions, or if they are out of time, they are
kicked to the final mark page.

Fair enough. That sounds like you indeed need something like you
described.
I've decided to place the current time in a php _SESSION variable and
then just use Javascript to compare the time in the _SESSION to the
current time. There is a problem through...

Is it possible to read a php session variable from javascript? Maybe
I should start a new thread. :)

You can't read or write PHP session data from javascript, and that's by
design - since you can't trust the client (browser). IOW you *will* need
to check the dates at the server end no matter what you do at the
browser side.

What you can do is pass the "current" value of any PHP variable to
javascript by generating a small js snippet:

<script>var mydata = <?php echo $myvar ?>;</script>

Or something like that, my PHP is pretty rusty.

In any case, your best bet is to pass the time not as a Date object but
as (micro)seconds since the epoch, since that's just a number and will
be much easier to pass between languages. See date.getTime()

HTH,
Joost.
 
F

fb

Joost said:
Fair enough. That sounds like you indeed need something like you
described.


You can't read or write PHP session data from javascript, and that's by
design - since you can't trust the client (browser). IOW you *will* need
to check the dates at the server end no matter what you do at the
browser side.

What you can do is pass the "current" value of any PHP variable to
javascript by generating a small js snippet:

<script>var mydata = <?php echo $myvar ?>;</script>

Or something like that, my PHP is pretty rusty.

In any case, your best bet is to pass the time not as a Date object but
as (micro)seconds since the epoch, since that's just a number and will
be much easier to pass between languages. See date.getTime()

HTH,
Joost.
Awesome...thanks!
 
D

Dr J R Stockton

Wed said:
In any case, your best bet is to pass the time not as a Date object but
as (micro)seconds since the epoch, since that's just a number and will
be much easier to pass between languages. See date.getTime()

Agreed, except that you have a funny way of spelling (milli)seconds.
But be wary of the possibility of a language usung the milliseconds as
local time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top