title: Cause hash to expire after a certain time
body: <p>I have a situation where a script receives some data from the user, it then returns a hash. For a certain amount of time, say X seconds, the hash is valid and when supplied back to the script along with the original data and within the specified timeframe, certain functions are allowed to be accessed.</p>
  
  <p>I am doing this by including a timestamp in the hash. I can get a timestamp by calling say <code>floor(time()/X);</code> but this, instead of expiring after X seconds, expires anywhere between 1 and X seconds. I have got around this by saving <code>time()%X</code> and appending it to the hash, then when I receive it back, parsing it out and subtracting it from <code>time()</code>, so my hash function looks a little like this:</p>
  
  <pre><code>function hash($oldhash='') {
      static $hash;
      if(!$hash) {
          $time = time();
          $expoff = explode('!', $oldhash, 2); 
          $expoff = $expoff[0] ? $expoff[0] : $time%$this-&gt;_cfg['hashexpire'];
          $hash = $expoff.'!'.sha1($this-&gt;_data.floor(($time-$expoff)/$this-&gt;_cfg['hashexpire']));
      }   
      return $hash;
  }
  </code></pre>
  
  <p><em>Theres also a salt in there but i removed it for clarity.</em></p>
  
  <p>This works, but I'm thinking there is probably a better way to achieve what I am after here and wonder if you have any suggestions.</p>