Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Generating random tokens

Does PhalconPHP has a build-in functionality to generate x-bytes random tokens? I need this to generate random tokens for the Remember me system.



6.3k
Accepted
answer

\Phalcon\Text::random($type, $length) but for this use case better use openssl_random_pseudo_bytes() and convert into an string with bin2hex() or base64_encode()

Ex:

<?php

function gen_token($bytes = 16) {
    return bin2hex(openssl_random_pseudo_bytes($bytes));
}

$token = gen_token(); // 128 bits random string