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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:16:04+00:00 2026-05-16T17:16:04+00:00

Can someone look at my two functions below and suggest what I can do?

  • 0

Can someone look at my two functions below and suggest what I can do? I have created two functions that basically creates a unique key and this is echoed in a hidden field in a form and then straight after I check if the form has been submitted the second function checks to see if the key in the hidden field matches the key in the session.

The problem I am having is now and again it just redirects me to to the forbidden page suggesting the keys don’t match although I have not edited the form key deliberately using my Firefox web dev tools to test.

I am not sure if it’s a cache issue or not, can anyone see if there is something that I am missing out or could improve on? It only happens now and again, for example if I submit the form a few times it may then just go to the forbidden page which suggests the key in hidden field did not match the key in the session, although I see nothing wrong with my two functions.

Here is my first function, this creates a unique key and this is echoed out in a hidden field in the form. I also have a time limit on how long a user has to submit the form but I have commented that out as of now because it seems to happen more often when enabled.

function GenerateFormTokenHash($token)
{
    $token = $_SESSION['token'] = md5(uniqid(mt_rand(), true));
    //$token_time = $_SESSION['token_time'] = time();
    return htmlspecialchars($token);
    //return $token_time;
}

To use the function above i simply echo GenerateFormTokenHash($token); in a hidden called token.

The function below is used straight after i check if the form has been submitted.

# Form Token Hash Validator
function IsValidFormTokenHash()
{
    /*global $websiteaddress;
        $token_age = time() - $_SESSION['token_time'];
        if($token_age >= 300) {
            echo 'Session Expired';
            echo 'This form has now expired. ';
            echo 'Please click here to go back to the form.';
            $_SESSION = array();
            setcookie(session_name(), '', time()-42000, '/');
            # Destroy the session
            session_destroy();
            # Generate new seesion id
            session_regenerate_id(true);
            exit;
        }*/
    if(isset($_POST['token']) && $_POST['token'] != $_SESSION['token'] || !isset($_POST['token']) || !isset($_SESSION['token']))
    {
                $_SESSION = array();
                setcookie(session_name(), '', time()-42000, '/');
                # Destroy the session
                session_destroy();
                # Generate new seesion id
                session_regenerate_id(true);
        redirect("/error/forbidden.php");
        exit;
    }
}

Again that function is in my functions.php file so after i check if form has been submitted i simply call the function as follows:

if(isset($_POST['submit'])) {
    IsValidFormTokenHash();
}

So I am basically trying to work out why sometimes now and then it just thinks the session key and key in hidden field does not match, maybe a cache issue or something I can do to ensure it works properly?

  • 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-16T17:16:05+00:00Added an answer on May 16, 2026 at 5:16 pm

    Probably what you need is to put parenthesis around your if check parts, they are probably getting evaluated in another way than your logic needs:

    if( (isset($_POST['token']) && $_POST['token'] != $_SESSION['token']) //<-- added parenthesis around those
      || !isset($_POST['token'])
      || !isset($_SESSION['token']))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can someone take a look at this please http://jsfiddle.net/bloodygeese/ecscY/56/ it looks like I have
Can someone look at this code. I am using two plugins in the form.
Trying to get this expression to work, can someone look at it and tell
Can someone please tell me what this means: 07-04 09:54:38.048: I/DetailActivity(15496): Title that is
I hope someone can offer any help!! I have web page that initiates a
I have an XML-like file that has lines that look like this: <siteMapNode title=Our
can someone give me a hint on how a histogram's pseudo code would look
Can someone tell me how to modify this regex to allow periods in a
can someone tell me what seems to be the issue with this <?php $increment
Can someone suggest a way to manipulate c++ objects with Cython, when the c++

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.