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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:59:04+00:00 2026-06-15T06:59:04+00:00

I have a php page that submits form to itself. I wanted to add

  • 0

I have a php page that submits form to itself.
I wanted to add a check where I can possibly stop basic cross site forgery.

my code:

<?php
session_start();

(...) //some code

$secret = substr(md5(uniqid(rand(), true)), 1, 2);
$_SESSION["secret"] = $secret;
$auth = $secret;

?>

(...)

<form id="form" name="form" action="<?php echo htmlentities($_SERVER[PHP_SELF]); ?>" method="post">

(...)

<input type="hidden" name="id" value="<? echo $auth; ?>" />

(...)

<?php

(...)

//as a debug I just tried to display
echo $_POST["id"];
echo "=";
echo $_SESSION["secret"];

(...)

?>
<div>
</body>
</html>

But both id and secret never returns the same value.
Why is that? What am I missing?


UPDATE

So, I tried an answer here by Samuel Cook, as also suggested by the comment of Jon Stirling:

if(!isset($_SESSION["secret"])){
    $secret = substr(md5(uniqid(rand(), true)), 1, 2);
    $_SESSION["secret"] = $secret;
    $auth = $secret;
}

But now if there is no $_SESSION["secret"]
The page returns:

<input type="hidden" name="id" value="" />

I understand why, its because secret is only being set if the session variable is present.
Any suggestions?


UPDATE

Moved the check before generating a new session key and is only checking if there is a post of id. works well now thanks.

  • 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-15T06:59:05+00:00Added an answer on June 15, 2026 at 6:59 am

    At the point when you debug code executes, the secret has changed:

    echo $_POST["id"]; // previous secret
    echo "=";
    echo $_SESSION["secret"]; // new secret
    

    As long as you do your check for a match at the beginning of the script (before generating the next secret) it will be fine.

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

Sidebar

Related Questions

I have an HTML form that submits to a PHP page which initiates a
I have a page that contains an HTML form that submits back to itself
I have a PHP page that queries a DB to populate a form for
I turned this case into a simple PHP page that submits to itself. My
I have this php page that posts to itself and then it checks weather
I have a form on one page that submits to another page. There, it
I have php page where I used a form to submit messaage. It submits
I have a PHP file that contains a form and which submits back to
I have a php page that generates a form. The action attribute of the
I have a PHP page that loads external content using other PHP files. I'm

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.