function getKey($username) { //Get their chat key, or generate a new one if we need to $query = pdo_prepare("SELECT `chatkey` FROM `users` WHERE `username` = :username"); $query->bind(":username", $username); $chatkey = $query->execute()->fetchIdx(0); if ($chatkey == "" || $chatkey == false) { //No key found, make them a new one $chatkey = strRand(32); $query = pdo_prepare("UPDATE `users` SET `chatkey` = :key WHERE `username` = :username"); $query->bind(":key", $chatkey); $query->bind(":username", $username); $query->execute(); } return $chatkey; }
<?php function strRand($length = 32, $characters = '0123456789abcdefghijklmnopqrstuvwxyxABCDEFGHIJKLMNOPQRSTUVWXYZ') { if (!is_int($length) || $length < 0) { return false; } $characters_length = strlen($characters) - 1; $string = ''; for ($i = $length; $i > 0; $i--) { $string .= $characters[mt_rand(0, $characters_length)]; } return $string; } print strRand(32);