如何实现一次性密码下载

时间:2018-03-01 11:02:43

标签: php one-time-password

我需要一些关于php的建议,我怎样才能让用户只能使用他们从我那里获得的密码下载文件一次。

首先,我需要一些带有唯一密码列表的连接数据库。 然后使用php我必须检测,如果用于下载密码 - 如果是,则禁用它。

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

您只需要在数据库中维护一个字段,这样当用户下次点击时,它就不会被下载。

我是这样做的: 当我发送链接时:

 $email_enc = base64_encode($check[0]->email_id);
    $id_enc = base64_encode($check[0]->id);
    $time = time();
    $url = $baseurl . "downloadfile?unq=" . $id_enc . "&em=" . $email_enc . "&tm=" . $time;

    $emaildata['message'] = "Hello $name,<br/><br/> Please <a href='$url'>Click</a> here to download your attachment.<br/><br/> Thanks,<br/>XYZ";
    sendmail($emaildata);
   //set status to 0

当用户点击链接时:

if ($_REQUEST['unq'] != '' && $_REQUEST['em'] != '' && $_REQUEST['tm'] != '') 
{
  $unique_id = base64_decode($_REQUEST['unq']);
  $u_email = base64_decode($_REQUEST['em']);
  $email_para = array(
    'id' => $unique_id
  );
  $check_avaibility = $this->User_model->getAnyData($email_para);
  if (!empty($check_avaibility)) 
  {
    $u_time = $_REQUEST['tm'];
    $cur_time = time();
    if ($cur_time - $u_time < 10800)
    {
      if(//check status to 0 only)
      {
       //download attachment
       //update the status as 1 that means link is not clickable next time
      }
    }
  }
}