public static function generateToken()
    {
        // Generate the token based on the current time, plus a random string.
        $token = time() . '.';

        // Start with an empty string.
        $length       = 32;
        $characterset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890';

        // For the desired length, keep adding random characters from the character set.
        for ($i = 0; $i < $length; $i++) {
            $token .= $characterset[mt_rand(0, strlen($characterset) - 1)];
        }

        $sessions = [];

        if (isset($_SESSION['csrftokens'])) {
            $sessions = array_slice($_SESSION['csrftokens'], -50, 50);
        }

        // Add the token to the "token-basket", a place in which we store all the currently valid CSRF tokens.
        $sessions[] = $token;

        $_SESSION['csrftokens'] = $sessions;

        return $token;
    }
public static function generateToken()
    {
        // Generate the token based on the current time, plus a random string.
        $token = time() . '.';

        // Start with an empty string.
        $length       = 32;
        $characterset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890';

        // For the desired length, keep adding random characters from the character set.
        for ($i = 0; $i < $length; $i++) {
            $token .= $characterset[mt_rand(0, strlen($characterset) - 1)];
        }

        $sessions = [];

        if (isset($_SESSION['csrftokens'])) {
            $sessions = array_slice($_SESSION['csrftokens'], -50, 50);
        }

        // Add the token to the "token-basket", a place in which we store all the currently valid CSRF tokens.
        $sessions[] = $token;

        $_SESSION['csrftokens'] = $sessions;

        return $token;
    }
public static function generateToken ( ) { // Generate the token based on the current time, plus a random string. $ token = time ( ) . '.' ; // Start with an empty string. $ length = 32 ; $ characterset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890' ; // For the desired length, keep adding random characters from the character set. for ( $ i = 0 ; $ i < $ length ; $ i ++ ) { $ token .= $ characterset [ mt_rand ( 0 , strlen ( $ characterset ) - 1 ) ] ; } $ sessions = [ ] ; if ( isset ( $ _SESSION [ 'csrftokens' ] ) ) { $ sessions = array_slice ( $ _SESSION [ 'csrftokens' ] , - 50 , 50 ) ; } // Add the token to the "token-basket", a place in which we store all the currently valid CSRF tokens. $ sessions [ ] = $ token ; $ _SESSION [ 'csrftokens' ] = $ sessions ; return $ token ; }
Generate a session token and store it in the "token-basket".
We return the generated token here, which can be used to set the hidden form field.

@return string
Generate a session token and store it in the token - basket . We return the generated token here which can be used to set the hidden form field .