I am making an authorization channel for a webservice in PHP. In the first two lines I make a random 18 bytes hex number that I set against the user in my database and also send the same auth_token to the user on the other end. But when the user sends the same auth_token to fetch some data, the script is unable to search the database. There is some problem with hex code that is queried against in the database. Please help.
$hex = bin2hex(openssl_random_pseudo_bytes('18'));
$database->executeObject('UPDATE tbluser SET user_auth_token="'.$hex.'" WHERE user_name="'.$_POST['uid'].'"');
...
...
...
}elseif ( $_POST['query'] = "fetch" && !empty($_POST['auth_token']) ){
$token = $_POST['auth_token'];
$uid = $database->executeObject('SELECT user_id AS id FROM tbluser WHERE user_auth_token="'.$token.'"');
if (!empty($uid)){
$fname = $database->executeObject('SELECT writer_first_name as fname FROM tblwriter WHERE user_id="'.$uid.'"')->fname;
echo $fname;
exit;
}else{ echo "Not Authorized"; exit; }