PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
create table otp ( phone varchar(64) primary key, otp int, type enum('new', 'old'), validity datetime );
Copy Clear
Copy Format Clear
<?php function generateOTP($theUser, $thePhone){ global $pdo; try { $stmt = $pdo->prepare(" INSERT INTO otp(phone, otp, type, validity) VALUES (:phone, :otp, :type, :val) ON DUPLICATE KEY UPDATE SET otp = :otp, type = :type, validity = :val"); $stmt-> bindValue(':phone', $thePhone); $stmt-> bindValue(':otp', rand(1000, 9999)); $stmt-> bindValue(':type', 'new'); $stmt-> bindValue(':val', date('Y-m-d H:i:s', strtotime('+5 mins'))); return $stmt-> execute(); } catch (PDOException $Exception ) { return false; } } function sendOTP($theUser, $thePhone){ global $pdo; return generateOTP($theUser, $thePhone); } $theUser = 'User'; $thePhone = '+123456789'; // CALLED SOMEWHERE LIKE THIS if(sendOTP($theUser, $thePhone){ echo "OTP SENT"; }else{ echo "OTP SENDING FAILED"; }
Show:  
Copy Clear