PHP-加密登录凭据访问API

时间:2016-04-12 15:14:29

标签: php api hash login

我正在尝试调用Harvest API。但是,为了做到这一点,用户名和密码必须放在PHP文件中。现在,我有安全问题,我不想直接将我的登录凭据放在一个文件中。我已经读过像SHA1,md5这样的散列机制。这适用于我的情况吗?因为我现在看的大部分示例都是基于验证登录凭据。在这里,我需要一种方法来隐藏我的登录凭据,以便它不会公开公开。请建议一种解决方法。

$user= //Actual username;
$password= //Actual Password
$harvest_user = $user; // Harvest username, usually an email address
$harvest_pass = $password; //  Harvest password
$harvest_account = $account;

require_once 'HarvestAPI.php';

spl_autoload_register(array('HarvestAPI', 'autoload') );

$harvestAPI = new HarvestAPI();
$harvestAPI->setUser($harvest_user);
$harvestAPI->setPassword($harvest_pass);
$harvestAPI->setAccount($harvest_account);

$result = $harvestAPI->getUsers();
// If Harvest returns successful
if ($result->code == 200) {
  $people = $result->data;
}
else{
echo "Not Successful";
}
?>

2 个答案:

答案 0 :(得分:-1)

您可以使用md5()方法。

只需在注册和存储数据库之后加密密码,或者在其他地方存储登录信息。

在登录时只需加密输入的密码,检查它是否与数据库中的密码相同。

没有人可以从数据库中获取密码,因为md5()方法不是 可逆的。

加密:$pw2store = md5($password);

或者用加密密码替换密码:$password = md5($password);

希望我能提供帮助。

答案 1 :(得分:-1)

if (!defined('access')): die('no access for you');

class HarvestAPI
{
    private $username;
    private $...;

    public function __construct($u,$...)
    {
        $this->username = $u;
    }

    public function start()
    {
        require_once 'HarvestAPI.php';
        spl_autoload_register(array('HarvestAPI', 'autoload') );
        $harvestAPI = new HarvestAPI();
        $harvestAPI->setUser($this->username);
        $harvestAPI->setPassword($....);
        $harvestAPI->setAccount($....);

        return $harvestAPI->getUsers();
    }
}

用法(在另一个文件中):

define('access',0);
require_once 'thatfileabove.php';
$result = (new HarvestAPI('username','ect..'))->start();
($result->code == 200)? $people = $result->data : "Not Successful";

除非有人查看您的代码,否则现在无法查看您的凭据。