I've created a logging system with one-use passwords (sent via SMS API). What I would like to know is if it's "safe". I know it is hard to estimate safety but I am curious if it is safer than regular password. I would appreciate any advice to improve following code.
Scheme is:
Logging:
Loggin at a webpage -> Ask external server for password -> Store the password in session var and when form is sent compare it with the sent one
External script:
When asked with proper apikey -> give random password and send it via SMS API
The code is below:
External script:
<?php
$apiKey='foobar123';
if ($_GET['api']!=$apiKey) {
header('HTTP/1.0 404 Not Found');
die();
}
$ile=12; //char number
$onepass='';
for($i=0;$i<$ile;$i++)
{
$k=rand(1,10);
$onepass.=$letter[$k];
}
if (!isset($_POST['ip'],$_POST['ip'])) {echo 'error'; die();}
$ip=$_POST['ip'];
$www=$_POST['www'];
echo $onepass;
include('sender.php'); //sending API
?>
Logging script (at page foo.com)
<?php
session_start();
$sessi='1231dsahsda8';
$www="foo.com";
$ip=$_SERVER['REMOTE_ADDR'];
if(isset($_GET['destroyer'])){
if($_GET['destroyer']=='yes'){
session_destroy(); echo 'Logged out - <a href="?relog">Log in again</a>'; die();
}
if($_GET['destroyer']=='no'){
session_destroy(); echo 'New pass has been sent!'; session_start();
}
}
if (isset($_POST['password'])){
if($_POST['password']==$_SESSION['pass']) $_SESSION[$sessi]="log";
else echo'Wrong Password <a href="?destroyer=no">Send again</a>';
}
if ($_SESSION[$sessi]!="log") {
if(!isset($_SESSION['pass'])){
$adres='http://www.foobar.com/index.php?api=foobar123';
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $adres);
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, "ip=$ip&www=$www");
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); /
$dane=curl_exec($c);
curl_close($c);
$_SESSION['pass']=$dane;
echo 'Password has been sent';
}
echo'<p>Log in</p><form action="?a" method="post"><input type="password" name="password" value=""/><input type="submit" value="Login" /></form>';
}
else{//authorised
echo 'Logged <p><a href="?destroyer=yes">Logout</a></p';
}
?>