<?php
class Entity extends PDO {
public function __construct() { try {
parent::__construct('mysql:host=localhost;dbname=X', 'db_user', 'db_pass');
parent::setAttributes(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) { die($e->getMessage()); }}
}
class DataSource {
public static function simple_select($sql, array $values) {
$smpt = (new Entity)->Prepare($sql);
$smpt->execute($values);
return $smpt->fetchAll();
}
}
class UniqueString {
private $Secret;
public function __construct($secret) {
$this->Secret = $secret;
}
public function fetch() {
return $this->Secret;
}
}
class Generate {
private static $Secret;
private static $Sql = 'SELECT user_id FROM users WHERE salt = ?';
public static function randomString(int $len) { do {
self::$Secret = mcrypt_create_iv($len, MCRYPT_DEV_URANDOM);
} while (!empty(DataSource::simple_select(self::$Sql, [self::$Secret])['user_id']));
return new UniqueString(self::$Secret);
}
}
echo Generate::randomString(16)->fetch();
Fatal error: Uncaught Error: Call to undefined function mcrypt_create_iv() in /in/jPCSd:35
Stack trace:
#0 /in/jPCSd(41): Generate::randomString(16)
#1 {main}
thrown in /in/jPCSd on line 35
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Fatal error: Uncaught Error: Call to undefined function mcrypt_create_iv() in /in/jPCSd:35
Stack trace:
#0 /in/jPCSd(41): Generate::randomString(16)
#1 {main}
thrown in /in/jPCSd on line 35
Process exited with code 255.
Output for 5.6.0 - 5.6.27
Catchable fatal error: Argument 1 passed to Generate::randomString() must be an instance of int, integer given, called in /in/jPCSd on line 41 and defined in /in/jPCSd on line 34
Process exited with code 255.