【问题标题】:Secure parameter in Codeigniter PHP controller?Codeigniter PHP控制器中的安全参数?
【发布时间】:2023-03-15 10:16:02
【问题描述】:

我正在尝试为 Coinbase 比特币支付创建回调脚本。这是我的支付控制器的以下功能:

function callback($secret = NULL) {
    if ($secret == 'testSECRETkey') {

    //If order is "completed", please proceed.

    $data = json_decode(file_get_contents('php://input'), TRUE);

    $status = $data['order']['status'];
    $userid = '507';

    if (($status === 'completed')) {
        $this->db->query( 'update users set user_money=user_money+15, user_credits=user_credits+5 WHERE users_id=' . $userid );
    }
}

如何包含special parameter,所以当我请求url时:www.example.com/payments/callback 添加特殊密钥,如果拒绝访问脚本无效。示例:

www.example.com/payments/callback?secret=testSECRETkey

不幸的是,它不能按我的意愿工作。它没有生效。有什么问题吗?

【问题讨论】:

    标签: php codeigniter parameters coinbase-api


    【解决方案1】:

    要访问key 参数,您将使用输入类的get 方法https://ellislab.com/codeigniter/user-guide/libraries/input.html

    $this->input->get('key');

    function callback()
    {
        $key = $this->input->get('key');
        if( ! $this->is_valid_key($key)) {
           // do something here and return
           return;
        }
    
        //If order is "completed", please proceed.
    
        $data = json_decode(file_get_contents('php://input'), TRUE);
    
        $status = $data['order']['status'];
        $userid = '507';
    
        if (($status === 'completed')) {
            $this->db->query( 'update users set user_money=user_money+15, user_credits=user_credits+5 WHERE users_id=' . $userid );
        }
    }
    

    创建一个新方法来验证密钥

    function is_valid_key($key) {
        // logic to check key
        $valid = true;
        if($valid) {
            return true;
        }
        else {
           return false;
        }
    }
    

    【讨论】:

    • 我这样做了,我正在这样使用它:http://www.example.com/payments/callback?key=true 但它根本不起作用。我的意思是它不会影响它。
    • 当你print_r($this->input->get());时会发生什么?
    • 确保您在application/config/config.php - - $config['allow_get_array'] = TRUE; 中允许GET
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    相关资源
    最近更新 更多