Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5838069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:25:43+00:00 2026-05-22T11:25:43+00:00

I have a situation where a script receives some data from the user, it

  • 0

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.

I am doing this by including a timestamp in the hash. I can get a timestamp by calling say floor(time()/X); but this, instead of expiring after X seconds, expires anywhere between 1 and X seconds. I have got around this by saving time()%X and appending it to the hash, then when I receive it back, parsing it out and subtracting it from time(), so my hash function looks a little like this:

function hash($oldhash='') {
    static $hash;
    if(!$hash) {
        $time = time();
        $expoff = explode('!', $oldhash, 2); 
        $expoff = $expoff[0] ? $expoff[0] : $time%$this->_cfg['hashexpire'];
        $hash = $expoff.'!'.sha1($this->_data.floor(($time-$expoff)/$this->_cfg['hashexpire']));
    }   
    return $hash;
}

Theres also a salt in there but i removed it for clarity.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:25:44+00:00Added an answer on May 22, 2026 at 11:25 am

    Well, you want it to expire after a certain number of seconds, correct? Well, why not pre-compute the expire time, and pass that instead? You’re only talking a few extra bytes being transferred:

    function makeHash() {
        $expire = time() + $this->_cfg['hashexpire'];
        $hash = $expire . ':' . hash_hmac('sha1', $this->_data, $expire);
        return $hash;
    }
    

    Then to verify:

    function verifyHash($hash) {
         if(!strpos($hash, ':')) return false;
         list ($expire, $rawhash) = explode(':', $hash, 2);
         $testhash = hash_hmac('sha1', $this->_data, $expire);
         if ($expire < time() && $testhash == $rawhash) {
             return true;
         }
         return false;
    }
    

    Note that it’s using a HMAC to “sign” the data payload.

    And if you’re really paranoid about the data size, just base_convert($expire, 10, 36) prior to sending it to the client, and then decode it when verifying it.

    Edit: As I’m rereading your question, I’m not sure. Are you looking for a hash to be generated that will be invalid after $x seconds? Or are you looking to generate the same hash for a absolute time window of $x seconds (after which the hash changes)? If the prior, the above solution will work. If the later, then you could try something like this:

    function makeHash() {
        $time = time();
        $startTime = $time - ($time % $this->_cfg['hashexpire']);
        return hash_hmac('sha1', $this->_data, $startTime);
    }
    

    The reason this works, is that it hashes with the timestamp of the first valid second of the present window. That will always stay the same, so there’s no need to store it or go any farther…

    Note that the generated hash will change only every $_cfg['hashexpire'] seconds. But it could be invalidated the very second after it was created. So only use this if that’s really what you need.
    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a situation in which a large amount of data output from Matlab
I have situation where a user can manipulate a large set of data (presented
At work, we have a situation where when script/server is run, then all the
Possible Duplicate: What is my script src URL? I have situation: <script type=text/javascript src=http://server/some.js>
I have a situation whereby when a page loads, I send some authentication data
I have some PHP script that logs in and returns a JSON array with
I have a situation where I need to get data from one of over
I have the following situation. I have a PHP script that imports a CSV
I`m currently working on a script, and I have the following situation. function somnicefunction()
I have situation in which I read a record from a database. And if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.