• 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!

TOPIC: Compile form only with single code

Compile form only with single code 1 week 3 days ago #44108

  • giovs978
  • giovs978's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 2
Helo
i need a forum that can be sended only if the user insert a onetime preshared code.
It's possible?
Thanks
The administrator has disabled public write access.

Compile form only with single code 5 days 9 hours ago #44115

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 267
  • Thank you received: 70
Yes, perfectly possible, two ways to do it, heres one that uses JSON which is ideal if you only need a few codes and don't need database management.
First create a text field 'preshared_code' label it Access Code or something suitable.

Then paste this in Php scripts - Pre Processing/ scripts called before form is validated
// --- One-time pre-shared code check (JSON-based) ---
 
$allowed_codes = ['ABC123', 'XYZ789', 'RSF2025'];  // <-- your valid one-time codes
 
$code = trim($form->data['preshared_code']);
$json_file = JPATH_SITE . '/used_codes.json';
 
// Ensure JSON file exists
if (!file_exists($json_file)) {
    file_put_contents($json_file, json_encode([]));
}
 
$used_codes = json_decode(file_get_contents($json_file), true);
if (!is_array($used_codes)) {
    $used_codes = [];
}
 
// Check code validity
if (in_array($code, $used_codes)) {
    $form->setError('preshared_code', '❌ This code has already been used.');
} elseif (!in_array($code, $allowed_codes)) {
    $form->setError('preshared_code', '❌ Invalid access code.');
} else {
    // ✅ Mark as used
    $used_codes[] = $code;
    file_put_contents($json_file, json_encode($used_codes, JSON_PRETTY_PRINT));
}

This is what happens
1. User enters a code - e.g. ABC123,
2. Script checks used_codes.json - If not present, it creates one
3. If code is not in $allowed_codes- Form shows “Invalid access code”
4. If code already in JSON Form shows “This code has already been used”

on first succesful submission the code creates a JSON file in your root folder, if you delete this you can use the $allowed_codes for testing as much as you like.
On the downside the JSON is not secure and could be accessed via a browserbut you could add to your htaccess file to prevent that or change the path to write it outside the root.
If you have access to your database and can create a table, that would be better for larger numbers of access codes and more secure.
If you can keep your head when all about you are losing theirs, then you obviously don't understand the situation!
The administrator has disabled public write access.
  • 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!