You are here

function _captcha_generate_captcha_session in CAPTCHA 8

Same name and namespace in other branches
  1. 6.2 captcha.inc \_captcha_generate_captcha_session()
  2. 7 captcha.inc \_captcha_generate_captcha_session()

Helper function for generating a new CAPTCHA session.

Parameters

string $form_id: The form_id of the form to add a CAPTCHA to.

int $status: The initial status of the CAPTHCA session.

Return value

string The session ID of the new CAPTCHA session.

2 calls to _captcha_generate_captcha_session()
Captcha::processCaptchaElement in src/Element/Captcha.php
Process callback for CAPTCHA form element.
CaptchaImageRefresh::refreshCaptcha in image_captcha/src/Controller/CaptchaImageRefresh.php
Put your code here.

File

./captcha.inc, line 79
General CAPTCHA functionality and helper functions.

Code

function _captcha_generate_captcha_session($form_id = NULL, $status = CAPTCHA_STATUS_UNSOLVED) {
  $user = \Drupal::currentUser();

  // Initialize solution with random data.
  $solution = hash('sha256', mt_rand());

  // Insert an entry and thankfully receive the value
  // of the autoincrement field 'csid'.
  $captcha_sid = \Drupal::database()
    ->insert('captcha_sessions')
    ->fields([
    'uid' => $user
      ->id(),
    'sid' => session_id(),
    'ip_address' => \Drupal::request()
      ->getClientIp(),
    'timestamp' => \Drupal::time()
      ->getRequestTime(),
    'form_id' => $form_id,
    'solution' => $solution,
    'status' => $status,
    'attempts' => 0,
  ])
    ->execute();
  return $captcha_sid;
}