添加检查到PHP功能?

时间:2014-09-23 03:19:23

标签: php codeigniter

我有以下基本功能:

    function buy()
    {
        $item_id = ( int )$this->uri->segment( 3 );
        if ( $item_id > '0' )
        {
            $item = $this->db->where( 'shop_id', $item_id )->get( 'shop' )->row();
            if ( $item )
            {
                $player = $this->user->info( $this->user->id() );

                    if ( $player->users_money >= $item->shop_req_money && $player->users_credits >= $item->shop_req_credits)
                    {

                    $this->db->update( 'users_items', array( 'users_id' => $this->user->id(), 'users_motors_id' => '0' ), array( 'users_items_id' => $item->users_items_id ) );
                    $this->db->update( 'users', array( 'users_money' => $player->users_money - $item->shop_req_money, 'users_credits' => $player->users_credits - $item->shop_req_credits ), array( 'users_id' => $this->user->id() ) );
                    $this->db->query( 'UPDATE users SET users_money=users_money+' . $item->shop_req_money . ', users_credits=users_credits+' . $item->shop_req_credits . ' WHERE users_id=' . $this->db->escape( $item->shop_users_id ) );
                    $this->db->delete( 'shop', array( 'shop_id' => $item->shop_id ) );
                    $this->session->set_flashdata( 'success', true );

                }
                else
                    $this->session->set_flashdata( 'error', true );
            }
        }
        header( 'Location: ' . $_SERVER['HTTP_REFERER'] );
    }

我编辑它就是为了添加额外的检查,如果reg ip与另一个不同,如果是,则设置错误信息。

function buy()
{
    $item_id = ( int )$this->uri->segment( 3 );
    if ( $item_id > '0' )
    {
        $item = $this->db->where( 'shop_id', $item_id )->get( 'shop' )->row();
        if ( $item )
        {
            $player = $this->user->info( $this->user->id() );
            $players = $this->user->info( $item->shop_users_id );


                if ( $players->users_reg_ip === $player->users_reg_ip )
                {
                $this->session->set_flashdata( 'errorip', true );
                }
                elseif ( $player->users_money >= $item->shop_req_money && $player->users_credits >= $item->shop_req_credits)
                {

                $this->db->update( 'users_items', array( 'users_id' => $this->user->id(), 'users_motors_id' => '0' ), array( 'users_items_id' => $item->users_items_id ) );
                $this->db->update( 'users', array( 'users_money' => $player->users_money - $item->shop_req_money, 'users_credits' => $player->users_credits - $item->shop_req_credits ), array( 'users_id' => $this->user->id() ) );
                $this->db->query( 'UPDATE users SET users_money=users_money+' . $item->shop_req_money . ', users_credits=users_credits+' . $item->shop_req_credits . ' WHERE users_id=' . $this->db->escape( $item->shop_users_id ) );
                $this->db->delete( 'shop', array( 'shop_id' => $item->shop_id ) );
                $this->session->set_flashdata( 'success', true );

            }
            else
                $this->session->set_flashdata( 'error', true );
        }
    }
    header( 'Location: ' . $_SERVER['HTTP_REFERER'] );
}

它按照它的方式工作但基本上我在问这是不是这样做的正确方法,或者应该有别的东西,我可以做到类似但更好?如果对我的问题有任何意见,我将非常感激。提前致谢!

1 个答案:

答案 0 :(得分:0)

是的,你可以在条件块中包括它。像这样:

if ( $players->users_reg_ip === $player->users_reg_ip) {
    // trying to buy from the same IP
    $this->session->set_flashdata( 'errorip', true );
}

elseif ( $player->users_money < $item->shop_req_money && $player->users_credits < $item->shop_req_credits) {
    // if user has not enough money and user credits is less than shop requirement credits
    // or maybe you mean OR ||
    $this->session->set_flashdata( 'error', true );

}
else {
    $this->db->update('users_items', 
        array( 'users_id' => $this->user->id(), 'users_motors_id' => '0' ), 
        array( 'users_items_id' => $item->users_items_id ) 
    );

    $this->db->update( 'users', 
        array( 'users_money' => $player->users_money - $item->shop_req_money, 
            'users_credits' => $player->users_credits - $item->shop_req_credits 
        ), 
        array( 'users_id' =>  $this->user->id() ) 
    );

    $this->db->query( 'UPDATE users SET users_money=users_money+' . $item->shop_req_money . ', users_credits=users_credits+' . $item->shop_req_credits . ' WHERE users_id=' . $this->db->    escape(     $item->shop_users_id ) );

    $this->db->delete( 'shop', array( 'shop_id' => $item->shop_id ) );
    $this->session->set_flashdata( 'success', true );
}

// instead of using referrer why not
// redirect('controller/method'); ?