PHP 8.4.0 RC4 available for testing

OAuthProvider::generateToken

(PECL OAuth >= 1.0.0)

OAuthProvider::generateTokenGenerate a random token

Опис

final public static OAuthProvider::generateToken(int $size, bool $strong = false): string

Generates a string of pseudo-random bytes.

Параметри

size

The desired token length, in terms of bytes.

strong

Setting to true means /dev/random will be used for entropy, as otherwise the non-blocking /dev/urandom is used. This parameter is ignored on Windows.

Значення, що повертаються

The generated token, as a string of bytes.

Помилки/виключення

If the strong parameter is true, then an E_WARNING level error will be emitted when the fallback rand() implementation is used to fill the remaining random bytes (e.g., when not enough random data was found, initially).

Приклади

Приклад #1 OAuthProvider::generateToken() example

<?php
$p
= new OAuthProvider();

$t = $p->generateToken(4);

echo
strlen($t), PHP_EOL;
echo
bin2hex($t), PHP_EOL;

?>

Поданий вище приклад виведе щось схоже на:

4
b6a82c27

Примітки

Зауваження:

When not enough random data is available to the system, this function will fill the remaining random bytes using the internal PHP rand() implementation.

Прогляньте також

add a note

User Contributed Notes 1 note

up
0
carlosouza at me dot com
12 years ago
Be careful when setting the 'strong' parameter to true.

If you system doesn't have enough entropy your script will block which can cause timeouts in other parts of your code.

In my case, the most serious symptom was my script blocking when trying to read from /dev/random and causing a 'MySQL has gone away' error.

Hopefully this saves someone the trouble when deciding to use /dev/random entropy
To Top