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

  • SEARCH
  • Home
  • 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 6896869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:05:53+00:00 2026-05-27T07:05:53+00:00

I am trying to integrate a method to add a hash token to every

  • 0

I am trying to integrate a method to add a hash token to every form in my application. I want to achieve to goals with this:

  1. Prevent csrf attacks
  2. Prevent that a form gets resubmitted when the user reloads a page after submitting a form

Now, the concept in doing this should be simple enough I thought:

  • I generate a unique hash and save it to the cookie
  • I create a hidden field in the form with the generated hash
  • before processing the $_POST data from the form I verify that the hash value from the form matches the one from the cookie.

So far so good, now what where I really get stuck is the following scenario:

What if a user opens another tab with with my application. Everytime the page gets loaded the hash value gets regenerated. Thus rendering the hash value from the form in the first tab invalid.

  • 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-27T07:05:53+00:00Added an answer on May 27, 2026 at 7:05 am

    Don’t store the token as a cookie.

    Generate a unique token on each page impression. Upon POST, verify that YOU issued the token (to this user) and verify that it hasn’t been used before.

    First, we generate a token where we can verify that we issued it for a given user:

    token = hash(session_id + secret)

    That way, using their session id and our secret, we can always verify that we issued that token, since noone knows secret.

    Now we need to make sure that the token can only be used once.

    rnd = rand()
    token = rnd + hash(session_id + secret + rnd)
    

    The token now has a random number. When a POST happens, we can store this random number as “has been used before” and reject any token that re-uses the same random number.

    But we don’t want to store random numbers of used tokens forever. So we limit the lifetime of tokens.

    rnd = rand()
    now = time()
    token = rnd + time + hash(session_id + secret + rnd + time)
    

    On POST, when we get a token we now check if we issued it “recently”. We only need to store used random numbers for that same timespan. All older tokens are invalid by definition.

    You can safe the used random numbers along with the session id and delete them when you evict the session id or when they become invalid (whichever happens first).

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

Sidebar

Related Questions

While trying to integrate Yahoo Media Player into my own website, I want to
I am trying to integrate a Hibernate application into a proprietary framework. My problem
I am trying to integrate REDBEAN into my web application. I would like my
I currently have an ASP.NET MVC application that exists. I want to add a
I'm trying to integrate an application with Facebook Chat in C#. Facebook recommends the
This is a simple page I am trying to add a .swf to, with
I have been trying the model-first method when designing my application. We usually like
I'm trying to iterate over an enum, and call a method using each of
I'm trying to integrate running Fitnesse tests from MSBuild im my nightly build on
We are trying to integrate tests in our daily builds using TestComplete, so far

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.