Skip to content

Commit 9c5d696

Browse files
committed
Adding csrf_field helper method
1 parent ffea677 commit 9c5d696

3 files changed

Lines changed: 30 additions & 0 deletions

File tree

system/Common.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,21 @@ function csrf_hash()
547547

548548
//--------------------------------------------------------------------
549549

550+
if (! function_exists('csrf_field'))
551+
{
552+
/**
553+
* Generates a hidden input field for use within manually generated forms.
554+
*
555+
* @return string
556+
*/
557+
function csrf_field()
558+
{
559+
return '<input type="hidden" name="'. csrf_token() .'" value="'. csrf_hash() .'">';
560+
}
561+
}
562+
563+
//--------------------------------------------------------------------
564+
550565
if (! function_exists('force_https'))
551566
{
552567
/**

user_guide_src/source/general/common_functions.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,15 @@ Miscellaneous Functions
137137

138138
Returns the current CSRF hash value.
139139

140+
.. php:function:: csrf_field ()
141+
142+
:returns: A string with the HTML for hidden input with all required CSRF information.
143+
:rtype: string
144+
145+
Returns a hidden input with the CSRF information already inserted:
146+
147+
<input type="hidden" name="{csrf_token}" value="{csrf_hash}">
148+
140149
.. php:function:: force_https ( $duration = 31536000 [, $request = null [, $response = null]] )
141150
142151
:param int $duration: The number of seconds browsers should convert links to this resource to HTTPS.

user_guide_src/source/libraries/security.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ and ``csrf_hash()`` functions
5757

5858
<input type="hidden" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>" />
5959

60+
Additionally, you can use the ``csrf_field()`` method to generate this
61+
hidden input field for you::
62+
63+
// Generates: <input type="hidden" name="{csrf_token}" value="{csrf_hash}" />
64+
<?= csrf_field() ?>
65+
6066
Tokens may be either regenerated on every submission (default) or
6167
kept the same throughout the life of the CSRF cookie. The default
6268
regeneration of tokens provides stricter security, but may result

0 commit comments

Comments
 (0)