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 7887039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:28:37+00:00 2026-06-03T05:28:37+00:00

So I am reading around and was really confused about having a CSRF token,

  • 0

So I am reading around and was really confused about having a CSRF token, whetever I should generate a new token per each request, or just per hour or something?

$data['token'] = md5(uniqid(rand(), true));
$_SESSION['token'] = $data['token'];

But let’s say it’s better to generate a token each hour, then I would need two sessions: token, expiration,

And how will I proceed it to the form? Just put echo $_SESSION[‘token’] on the hidden value form and then compare on submit?

  • 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-06-03T05:28:38+00:00Added an answer on June 3, 2026 at 5:28 am

    If you do it per form request – then you basically remove the ability for CSRF attacks to occur & you can solve another common issue: multiple form submission

    In simple terms – your application will only accept form input if the user ASKED for the form prior to the submission.

    Normal scenario:
    User A goes to your website, and asks for Form A, is given Form A plus a unique code for Form A. When the user submits Form A, he/she must include the unique code which was only for Form A.

    CSRF Attack scenario: User A goes to your website, and asks for Form A. Meanwhile they visit another “bad” site, which attempts a CSRF attack on them, getting them to submit for a fake Form B.

    But your website knows that User A never asked for Form B – and therefore even though they have the unique code for Form A, Form B will be rejected, because they dont have a Form B unique Code, only a Form A code. Your user is safe, and you can sleep easy at night.

    But if you do it as a generic token, lasting for an hour (like you posted above) – then the attack above might work, in which case you’ve not achieved much with your CSRF protection. This is because the application does not know that form B was never asked for in the first place. It is a generic token. The WHOLE POINT of CSRF prevention is to make each form token unique to that form

    Edit: because you asked for more information:
    1 – you dont have to do it per form request, you can do it per hour/session etc. The point is a secret value that is given to the user, and resubmiited on the return. This value is not known by another website, and thus cannot submit a false form.

    So you either generate the token per request, or per session:

    // Before rendering the page:
    $data['my_token'] = md5(uniqid(rand(), true));
    $_SESSION['my_token'] = $data['my_token'];
    
    // During page rendering:
    <input type="hidden" name="my_token" id="my_token" value="<? php echo $_SESSION['my_token']?>" />
    
    // After they click submit, when checking form:
    if ($_POST['my_token'] === $_SESSION['my_token'])
    {
            // was ok
    }
    else
    {
              // was bad!!!
    }
    

    and because it is “per form” – you wont get double form submissions – because you can wipe the token after the first form submission!

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

Sidebar

Related Questions

I've been reading around the web about different alternatives to keeping track of users
I really can't get my head around Activity, Intent and start.. even after reading
I remember reading some books about Linux Device drivers around the end of my
I've been reading about memory leakages lately and haven't yet wrapped my head around
I'm having several issues based around reading and writing an NSArray to and from
After reading a bunch of stackoverflow posts and forums I got really confused on
I've been reading about Inversion of Control frameworks and I'm just playing around with
I'm reading some data for countries around the world and am playing with Google's
I'm reading a local file using a BufferedReader wrapped around a FileReader: BufferedReader reader
I am trying to wrap my head around PHP OOP and after reading everything

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.