![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP program to generate a numeric one-time password
To generate a numeric one-time password in PHP, the code is as follows −
Example
<?php function generate_otp($n) { $gen = "1357902468"; $res = ""; for ($i = 1; $i <= $n; $i++) { $res .= substr($gen, (rand()%(strlen($gen))), 1); } return $res; } $num = 8; print_r("The one time password generated is :"); print_r(generate_otp($num)); ?>
Output
The one time password generated is :52471609
A function named ‘generate_otp’ is defined that takes the length as a parameter. This is the length of the password that needs to be generated. A number that contains 0 to 9 numbers is defined and the length is iterated over and a random number that contains these 0 to 9 numbers randomly is generated. The length is defined and the function is called on this length. This generates a numeric password and displays it on the console.
Advertisements