dos3018 2014-09-23 03:19
浏览 38
已采纳

添加检查到PHP功能?

I've got this basic function as below:

    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'] );
    }

and I edited it like that to add additional check if the reg ip isn't the same as the other one and if so to set an error message.

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'] );
}

It works the way it is BUT basically I am asking if thats the correct way of doing that or there should be something else I can do similiar to that but better? Would be really appreciated if there any comments on my question. Thanks in advance!

  • 写回答

1条回答 默认 最新

  • douningzhi1991 2014-09-23 03:30
    关注

    Yes you can include that on the condition block. Like this:

    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'); ?
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多