I need some way to generate a unique token that can be passed to a MySQL database, and stored there until it's manually reset. So it needs to be a random token that will be displayed to a user which should stay valid, but also have some function that can reset it - like a "reset key" link to automatically update, change, and then display the new token.
I was trying out this code, but it refreshes each time the page reloads:
global $wpdb;
global $user_login;
$token = uniqid();
$hashedtoken = md5($token);
$user = $user_login;
$wpdb->insert('wp_tokens',
array('user' => $user, 'token' => $hashedtoken),
array('%s','%s')
);
echo $hashedtoken;
It might be obvious I am using Wordpress, if that matters. I'm not even sure PHP is the best option here.
Edit: My question was flagged as being similar to this, and some cool people have notified that I need to query the database to check if the token exists for the user. However, no clue how to do these. Thanks.