3

I want to get the current time when a page loads for first time and then to compare that time to the current time only if the difference is more than 30 minutes from the first time the page was loaded, a new window should be opened. I wanted to ask that to do all this what should i use session or cookies? I have tired using the following:

<input type="hidden" name="time_last_loaded" value="<?php echo strtotime(date("Y-m-d H:i:s")); ?>">

<?
if(isset($_POST['time_last_loaded']))
{
    $current_time = strtotime(date("Y-m-d H:i:s"));
    $time_last_loaded_plus_30_mins = strtotime('+30 minutes',  $_POST['time_last_loaded']));
    if($time_last_loaded_plus_30_mins <  $current_time)
    {
             echo "code works";
     }
}
?>

But it is not working, could someone try to find a problem in what I have coded? Thank you!

i also tried this

$time=time();
$timeend= $time + (1*60);
 for($i=1;$i<100;$i++)
  {
  while($time > $timeend)
  {
      echo "it works";
      $time=time();
      }
  }

y this simple piece of code not working? can any1 tell?

  • 1
    Use cookies if the data is trivial and no harm can be done if it is manipulated, or otherwise use sessions. You'll find plenty of tutorials on what they are and how they work on Google, and some of them are even right :) – Mahn Jul 15 '12 at 07:32
  • You have to consider too the scope of what you are intending. Now everyone has cookies turned on in the first place and some mobile phones do not support them. I am in no way implying that you should not use cookies, or that they a worse option. I am just saying that it really depends on what you are using them for. In this case though, I would suggest a cookie! – Craig van Tonder Jul 15 '12 at 09:16
  • you could destroy the cookie after 30 minutes of inactivity, that way it is it not destroyed then well, you have your answer... There is a very good example of this relating to sessions but I am sure the same would apply: http://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes (last insert in the answer by Gumbo) – Craig van Tonder Jul 15 '12 at 09:24
  • Your second code snippet is incorrect. The code will never execute because the condition you have used is such. $time will always be less than $timeend. And $time is never getting updated because the line that updates it is inside the while loop. The interpreter never reaches there. Change your condition from "greater than (>)" to "less than (<)". – vaidik Jul 15 '12 at 10:23

2 Answers2

6

client side.cookies is the way to go. it will let the server un-aware(in terms of memory and processing time) of the whats going on, on the client. if the window is supposed to be opened on the client through javascript and you don't have anything to do with the stored time on the server.

Raab
  • 34,778
  • 4
  • 50
  • 65
  • but the user can destroy cookie isnt it? i dont want the user to destroy it. my application is a chatbot, an interviewing bot, and i want the interview to end after 30mins... – Jawaria Irfan Jul 15 '12 at 07:40
  • Well, the user can destroy the Sessions cookie as well. Sessions are also implemented using cookies. That can never be in your control unless you are doing something like actually storing the last accessed time in a database across every unique user. – vaidik Jul 15 '12 at 07:42
2

Sessions are generally used to identify client uniquely (something like user login session) for your application logic. If you don't have to identify a user uniquely, I'd suggest you use Cookies. You can use sessions as well but that will unnecessarily do some processing at the server end which looks like will not be of any use to you. Its up to you to decide. But if it is not required for you to identify a user uniquely, then just go for Cookies.

"i will compare that time to the current time if the difference is more than 30 minutes new window will be opened". I didn't quite get what you exactly mean by this so I will try to answer your question generally.

New window will be opened can mean two things: open a new window (target="_blank") or redirect to a new URL. If you want to do the first one, then the time processing that you want to do will have to be done and handled by a Javascript because you cannot open a new window in the browser from server side. If you want to do the second one, you can implement that either on the client side using JS or on server side using whatever language you are using.

As for your code snippet, I think you are not using strtotime() properly. Read this: http://php.net/manual/en/function.strtotime.php

I have made a minor change in your code. This should work but I have not tried it by myself: ">

<?
if(isset($_POST['time_last_loaded']))
{
    $current_time = strtotime(date("Y-m-d H:i:s"));
    $time_last_loaded_plus_30_mins = strtotime($_POST['time_last_loaded']) + 30*60;
    if($time_last_loaded_plus_30_mins <  $current_time)
    {
             echo "code works";
    }
}
?>
vaidik
  • 2,191
  • 1
  • 16
  • 22
  • Try this code snippet: `$time_last_loaded_plus_30_mins = strtotime(date("Y-m-d H:i:s")) + 30*60;` `sleep(2);` `$current_time = strtotime(date("Y-m-d H:i:s"));` `print $current_time . '\n' . $time_last_loaded_plus_30_mins;` `if($time_last_loaded_plus_30_mins < $current_time)` `{ echo "code works"; }` This one works fine for me so if this works for you, then perhaps there is something wrong with how you are sending the data from the browser to your server. So to debug that, I suggest echo the POST variable and see if the data that you are getting is what you expect. – vaidik Jul 15 '12 at 10:28