I have a front-end form that posts through the ID of a particular element/entry to a back-end controller. Is there any way to encrypt/hash that ID on the front-end, so the plain ID number is not visible, and then decrypt that ID in the back-end controller?
2 Answers
<input type="hidden" name="foo" value="{{ 'bar'|hash }}">
and then in your controller:
if (($foo = craft()->request->getPost('foo')) !== null)
{
$foo = craft()->security->validateData($foo);
}
at which point $foo will either be null
(not present), false
(present but tampered with), or a string (present and valid)
I've used hashids successfully before, then create a small plugin helper with a twig filter that transforms the id into a hash that also decrypts it. you can include composer to craft plugins by just updating your composer file and adding hashids to it.