Generate a simple code for mobile number verification

Hello,

I'm building a social network that will allow users to send SMSs between them, but the mobile number of each user must be first verified. Therefore I need a PHP script to do the following:

1- Once the user has typed his mobile number, the script generates a simple code (like "634928") which will be valid only for that number and not another one and for a concrete time (1 minute, for example).

2- Then that code is sent via SMS through the gateway I use and received by the number's owner. (For this step I just need a variable which takes the verification code, and I'll make it to be sent via SMS).

3- He writes it in a form which will check if the code is valid (the same generated at the beginning) and if it's still within the predefined time (the one set before).

4- If everything is OK a variable called "$verified" will take "yes" as value, if not, the page will ask to check the code.

I hope it's easy to do...

Thanks!
loggindevelopAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
abijelicConnect With a Mentor Commented:
You actualy need two pages.. one for creating and one for verifying or actualy  you can do it  in one page which recursively call itself.  or through javascript if its all on one page and in lets say 5 minutes timeframe

You could do something like this...   i have on purposly written with long variable names and coments so you could easly see what is going on

file 1 :  createRandomCode.php   and copy below code into it
<?
function make_seed() {
    list($usec, $sec) = explode(' ', microtime());
    return (float) $sec + ((float) $usec * 100000);
};

function MakeRandomCode() {
$ResultKode="";
srand(make_seed());
$CounterX=0;

while ($CounterX<9):  // until length of random code equals 8 signs <9
{
$TemporaryRandomCode=rand(65,90); // choose random number in ASCII range

$RandomLetter=chr($TemporaryRandomCode);  // convert it to character

$ResultKode .=$RandomLetter;   //Add random character to ResultString

$counterX=$counterX+1;

};
endwhile;
return $ResultKode;
};
?>

create file  SendRandomSMScode:
<?
include("createRandomCode.php");
$mobileNumber='the mobile number';  // to this variable you add the mobile number you receive through form
SmsCode=MakeRandomCode();
$SQLCode="Insert into SMScode (SMSCode,Mobilenumber) values ("  . $SmsCode . "," . $mobileNumber . ");" ;
mysql_query($SQLCode,"NAMEOFDATABASE")

//Insert code to send code to SMS
 ?>

Now you just need to read data from database and if everything is ok set the result  to $verified.

if you need more explanation  feel free to contact me...

1
 
loggindevelopAuthor Commented:
Hello, abijelic,

Thank you for your so fast answer, but I started trying it, exacuting first the MakeRandomCode() function and it gave me an error: max execution time limit exeeded (30 sec). I think there's an error in the code, but I don't know where. Please, could you check everything?

Thanks!
0
 
abijelicConnect With a Mentor Commented:
Yup.... fast is always wrong...
just fix the following line

$counterX=$counterX+1;  
and change it to
$CounterX=$CounterX+1;

The error is in fact that in one instance i typed CounterX with big C, and in another with small c :)
0
 
loggindevelopAuthor Commented:
Thank you!
0
 
abi jayaCommented:
Hi I am newbie to this field. How to run that above php code.

localhost /otpcode/SendRandomSMScode.php -  when i was run that code its displaying empty.
0
All Courses

From novice to tech pro — start learning today.