Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other Can someone recommend a customizable contact form, please? Reply To: Can someone recommend a customizable contact form, please?

#168890
__
Participant

No problem.

Here’s some of the functions we use in htmlMarkup, but haven’t defined yet.

First, we need to be able to pick random antispam question: answer pairs. We’ll define a map of questions to choose from. (This is just an example list; add more as desired. The more to choose from, the better.) The question will be the array key, and the answer will be the array value*:

protected $_antispam = array(
    "What color is the grass?" => "green",
    "What is 1+1?            " => "2"
    //  etc. ...
);

Then, our method will simply choose one of the questions:

/**
 * selects a random anti-spam question for use as an antispam challenge.
 *
 * @return string                   a challenge question
 */
protected function _generateAntispam(){
    return array_rand( $this->_antispam );
}

Next, we need a _generateToken method. This gives us a (reasonably) unique/random string to identify the form by.

/**
 * generates a random hash for use as a nonce token.
 *
 * @return string                   32-char hexadecimal hash
 */
protected function _generateToken(){
    // generate a unique nonce using a random bytestring + the current time
    // then hash it
    $token = md5( openssl_random_psuedo_bytes( 32 ).$this->_time );
    // save to the session: use the token as the key, and the current time as the value
    //  (so we know when the form was requested)
    $_SESSION[__CLASS__][$token] = $this->_time;
    return $token;
}

You might have noticed we’re using the “current time” in the code above. We need to add that property to our class:

protected $_time;

…and we’ll give it the current time in the class constructor (not written yet) using the microtime function.

Updated gist.